Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Ensure your development environment meets the system requirements.
Optionally, you may want to install the ArcGIS Maps SDK for .NET to get access to project templates in Visual Studio (Windows only) and offline copies of the NuGet packages.
Steps
Your app needs an access token
-
Complete the Create an API key tutorial to get a new API key access token
An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. .-
Ensure that the necessary privileges
Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. are enabled for the functionality your app requires. If you need to use basemaps, for example, enable access to the Basemap styles service: Location services > Basemaps > Basemap styles service. -
Copy the access token for use in the next step.
-
-
Add the API key access token to your app using the instructions below for either WPF or .NET MAUI.
-
In your project’s Solution Explorer, expand the node for App.xaml, and double-click App.xaml.cs to open it.
-
In the App class, add an override for the
OnStartup()function to set theApiKeyproperty onArcGISRuntimeEnvironment.App.xaml.cspublic partial class App : Application{protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);// Set the access token for ArcGIS Maps SDK for .NET.Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ApiKey = "YOUR_ACCESS_TOKEN";}}} -
Replace “YOUR_ACCESS_TOKEN” in the example above with the API key access token you created earlier.
-
Save and close the
App.xaml.csfile.
-
In your project’s Solution Explorer, open MauiProgram.cs by double-clicking.
-
Add the following required
usingdirectives at the top of the file.MauiProgram.cs13 collapsed lines// Copyright 2023 Esri// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// https://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.namespace DisplayAMap;using Esri.ArcGISRuntime;using Esri.ArcGISRuntime.Maui;using Microsoft.Extensions.Logging;public static class MauiProgram{public static MauiApp CreateMauiApp(){17 collapsed linesvar builder = MauiApp.CreateBuilder();builder.UseMauiApp<App>().ConfigureFonts(fonts =>{fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");});#if DEBUGbuilder.Logging.AddDebug();#endifreturn builder.Build();}} -
Use the existing
buildervariable in theCreateMauiApp()function to callUseArcGISRuntime(). Use a lambda function to callUseApiKey()on aconfigparameter to set theApiKeyproperty onArcGISRuntimeEnvironment.MauiProgram.cs25 collapsed lines// Copyright 2023 Esri// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// https://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.namespace DisplayAMap;using Esri.ArcGISRuntime;using Esri.ArcGISRuntime.Maui;using Microsoft.Extensions.Logging;public static class MauiProgram{public static MauiApp CreateMauiApp(){var builder = MauiApp.CreateBuilder();builder.UseMauiApp<App>().ConfigureFonts(fonts =>{fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");});builder.UseArcGISRuntime(config =>{config.UseApiKey("YOUR_ACCESS_TOKEN");});#if DEBUGbuilder.Logging.AddDebug();#endif4 collapsed linesreturn builder.Build();}} -
Update the
"YOUR_ACCESS_TOKEN"placeholder in the example above with the API key access token you created earlier. -
Save and close the MauiProgram.cs file.
-
For more information, see API key authentication.
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.