Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

ArcGIS token challenge

This code sample is available for these platforms:
View Sample on GitHub

This sample demonstrates how to prompt the user for a username and password to authenticate with ArcGIS Server to access an ArcGIS token-secured service. Accessing secured services requires a login that's been defined on the server.

screenshot

Use case

Your app may need to access services that are restricted to authorized users. For example, your organization may host ArcGIS services that are only accessible by verified users.

How it works

  1. A custom ChallengeHandler is set for AuthenticationManager that displays a login dialog for entering a username and password.
  2. In response to the attempt to access secured content, the AuthenticationManager calls the challenge handler.
  3. A TokenCredential is created from the entered username and password, and an attempt is made to load the layer.

Relevant API

  • AuthenticationManager
  • TokenCredential

Tags

Authentication, Security, Token

Sample Code

<UserControl x:Class="ArcGISRuntime.UWP.Samples.TokenSecuredChallenge.TokenSecuredChallenge"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="using:ArcGISRuntime.UWP.Samples.TokenSecuredChallenge"
             xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <UserControl.Resources>
        <local:LoadStatusToColorConverter x:Key="StatusToColor" />
    </UserControl.Resources>
    <Grid x:Name="layoutGrid">
        <esriUI:MapView x:Name="MyMapView" />

        <!-- Layer listing with status -->
        <Border Style="{StaticResource BorderStyle}">
            <StackPanel>
                <TextBlock FontSize="14" FontWeight="Bold"
                           Margin="4"
                           TextAlignment="Center"
                           Text="Map Layers (with status)" />
                <StackPanel x:Name="PublicLayerPanel" Orientation="Horizontal" Margin="10,5">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding LoadStatus}"
                               Foreground="{Binding LoadStatus, Converter={StaticResource StatusToColor}}"
                               Margin="10,0" />
                </StackPanel>
                <StackPanel x:Name="SecureLayerPanel" Orientation="Horizontal" Margin="10,5">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding LoadStatus}"
                               Foreground="{Binding LoadStatus, Converter={StaticResource StatusToColor}}"
                               Margin="10,0" />
                </StackPanel>
            </StackPanel>
        </Border>

        <!-- Login UI -->
        <Border x:Name="loginPanel"
                Style="{StaticResource BorderStyle}"
                HorizontalAlignment="Left"
                Visibility="Collapsed">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
                           Margin="0,0,0,8"
                           TextWrapping="Wrap"
                           Text="{Binding ServiceUrl}" />
                <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
                           Margin="0,0,0,8"
                           TextWrapping="Wrap"
                           FontWeight="SemiBold"
                           Text="Username and Password are user1/user1" />
                <TextBlock Grid.Row="2" Grid.Column="0"
                           Margin="2"
                           VerticalAlignment="Center"
                           Text="Username" />
                <TextBox Grid.Row="2" Grid.Column="1"
                         Margin="2"
                         Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                <TextBlock Grid.Row="3" Grid.Column="0"
                           Margin="2"
                           VerticalAlignment="Center"
                           Text="Password" />
                <!-- For simplicity, the password is visible in this example. You can use a PasswordBox to mask the password text,
                     but it complicates data binding. See this discussion for details: http://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm -->
                <TextBox Grid.Row="3" Grid.Column="1"
                         Margin="2"
                         Text="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
                        Margin="0,12,0,0"
                        HorizontalAlignment="Stretch"
                        Content="Login and load layer"
                        Click="LoginButtonClick" />
                <TextBlock x:Name="ErrorMessageTextBlock"
                           Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2"
                           Margin="8,12,8,0"
                           Foreground="Red"
                           TextWrapping="Wrap"
                           Text="{Binding ErrorMessage}" />
            </Grid>
        </Border>

    </Grid>
</UserControl>
// Copyright 2017 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: http://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.

using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Security;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;

namespace ArcGISRuntime.UWP.Samples.TokenSecuredChallenge
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
           "ArcGIS token challenge",
           "Security",
           "This sample demonstrates how to authenticate with ArcGIS Server using ArcGIS Tokens to access a secure service. Accessing secured services requires a login that's been defined on the server.",
           "1. When you run the sample, the app will load a map that contains a layer from a secured service.\n2. You will be challenged for a user name and password to view that layer.\n3. Enter the correct user name (user1) and password (user1).\n4. If you authenticate successfully, the secured layer will display, otherwise the map will contain only the public layers.",
           "Authentication, Security, ArcGIS Token")]
    public partial class TokenSecuredChallenge
    {
        // Public and secured map service URLs.
        private string _publicMapServiceUrl = "https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
        private string _secureMapServiceUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA_secure_user1/MapServer";

        // Public and secured layer names.
        private string _publicLayerName = "World Street Map - Public";
        private string _secureLayerName = "USA - Secure";

        // Task completion source to track a login attempt.
        private TaskCompletionSource<Credential> _loginTaskCompletionSource;

        public TokenSecuredChallenge()
        {
            InitializeComponent();

            // Define a method to challenge the user for credentials when a secured resource is encountered.
            AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(Challenge);

            // Call a function to create a map and add public and secure layers.
            Initialize();
        }

        private void Initialize()
        {
            // Create the public layer and provide a name.
            ArcGISTiledLayer publicLayer = new ArcGISTiledLayer(new Uri(_publicMapServiceUrl))
            {
                Name = _publicLayerName
            };

            // Set the data context for the public layer stack panel controls (to report name and load status).
            PublicLayerPanel.DataContext = publicLayer;

            // Create the secured layer and provide a name.
            ArcGISMapImageLayer tokenSecuredLayer = new ArcGISMapImageLayer(new Uri(_secureMapServiceUrl))
            {
                Name = _secureLayerName
            };

            // Set the data context for the secure layer stack panel controls (to report name and load status).
            SecureLayerPanel.DataContext = tokenSecuredLayer;

            // Create a new map and add the layers.
            Map myMap = new Map();
            myMap.OperationalLayers.Add(publicLayer);
            myMap.OperationalLayers.Add(tokenSecuredLayer);

            // Add the map to the map view.
            MyMapView.Map = myMap;
        }

        private async Task<Credential> Challenge(CredentialRequestInfo info)
        {
            // Get user credentials (on the UI thread).
            if (this.Dispatcher == null)
            {
                // No current dispatcher, code is already running on the UI thread.
                return await GetUserCredentialsFromUI(info);
            }
            else
            {
                // Use the dispatcher to invoke the challenge UI.
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                {
                    try
                    {
                        await GetUserCredentialsFromUI(info);
                    }
                    catch
                    {
                        // Login was canceled or unsuccessful, dialog will close.
                    }
                });
            }

            // Use the task completion source to return the results.
            return await _loginTaskCompletionSource.Task;
        }

        private async Task<Credential> GetUserCredentialsFromUI(CredentialRequestInfo info)
        {
            // Show the login UI.
            try
            {
                // Create a new LoginInfo to store the entered username and password.
                // Pass the CredentialRequestInfo object so the resource URI can be stored.
                LoginInfo loginInputInfo = new LoginInfo(info);

                // Set the login UI data context with the LoginInfo.
                loginPanel.DataContext = loginInputInfo;

                // Show the login UI.
                loginPanel.Visibility = Visibility.Visible;

                // Create a new task completion source to return the user's login when complete.
                // Set the login UI data context (LoginInfo object) as the AsyncState so it can be retrieved later.
                _loginTaskCompletionSource = new TaskCompletionSource<Credential>(loginPanel.DataContext);

                // Return the task from the completion source.
                // When the login button on the UI is clicked, the info will be returned for creating the credential.
                return await _loginTaskCompletionSource.Task;
            }
            finally
            {
                // Hide the login UI.
                loginPanel.Visibility = Visibility.Collapsed;
            }
        }

        // Handle the click event for the login button on the login UI.
        private async void LoginButtonClick(object sender, RoutedEventArgs e)
        {
            // Make sure there's a task completion source for the login operation.
            if (_loginTaskCompletionSource == null || _loginTaskCompletionSource.Task == null || _loginTaskCompletionSource.Task.AsyncState == null)
            {
                return;
            }

            // Get the login info from the task completion source.
            LoginInfo loginEntry = (LoginInfo)_loginTaskCompletionSource.Task.AsyncState;

            try
            {
                // Create a token credential using the provided username and password.
                TokenCredential userCredentials = await AuthenticationManager.Current.GenerateCredentialAsync
                                            (new Uri(loginEntry.ServiceUrl),
                                             loginEntry.UserName,
                                             loginEntry.Password,
                                             loginEntry.RequestInfo.GenerateTokenOptions);

                // Set the result on the task completion source.
                _loginTaskCompletionSource.TrySetResult(userCredentials);
            }
            catch (Exception ex)
            {
                // Show exceptions on the login UI.
                loginEntry.ErrorMessage = ex.Message;

                // Increment the login attempt count.
                loginEntry.AttemptCount++;

                // Set an exception on the login task completion source after three login attempts.
                if (loginEntry.AttemptCount >= 3)
                {
                    // This causes the login attempt to fail.
                    _loginTaskCompletionSource.TrySetException(new Exception("Exceeded the number of allowed login attempts"));
                }
            }
        }
    }

    // Helper class to contain login information.
    internal class LoginInfo : INotifyPropertyChanged
    {
        // Information about the current request for credentials.
        private CredentialRequestInfo _requestInfo;
        public CredentialRequestInfo RequestInfo
        {
            get { return _requestInfo; }
            set
            {
                _requestInfo = value;
                OnPropertyChanged();
            }
        }

        // URI for the service that is requesting credentials.
        private string _serviceUrl;
        public string ServiceUrl
        {
            get { return _serviceUrl; }
            set
            {
                _serviceUrl = value;
                OnPropertyChanged();
            }
        }

        // Username entered by the user.
        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OnPropertyChanged();
            }
        }

        // Password entered by the user.
        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged();
            }
        }

        // Last error message encountered while creating credentials.
        private string _errorMessage;
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                _errorMessage = value;
                OnPropertyChanged();
            }
        }

        // Number of login attempts.
        private int _attemptCount;
        public int AttemptCount
        {
            get { return _attemptCount; }
            set
            {
                _attemptCount = value;
                OnPropertyChanged();
            }
        }

        // Store the credential request information when the class is constructed.
        public LoginInfo(CredentialRequestInfo info)
        {
            RequestInfo = info;
            ServiceUrl = info.ServiceUri.AbsoluteUri;
            ErrorMessage = string.Empty;
            AttemptCount = 0;
        }

        // Raise a property changed event so bound controls can update.
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // Status to Color converter used by some UI elements.
    public class LoadStatusToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            Color statusColor;

            // Show red for unloaded (or failed), green for loaded, and gray for loading.
            switch ((int)value)
            {
                case (int)Esri.ArcGISRuntime.LoadStatus.Loaded:
                    statusColor = Colors.Green;
                    break;
                case (int)Esri.ArcGISRuntime.LoadStatus.Loading:
                    statusColor = Colors.Gray;
                    break;
                case (int)Esri.ArcGISRuntime.LoadStatus.FailedToLoad:
                    statusColor = Colors.Red;
                    break;
                case (int)Esri.ArcGISRuntime.LoadStatus.NotLoaded:
                    statusColor = Colors.Red;
                    break;
                default:
                    statusColor = Colors.Gray;
                    break;
            }

            return new SolidColorBrush(statusColor);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}