AnsweredAssumed Answered

Getting 401 (Unauthorized) accessing secured service with valid credentials

Question asked by alehmann on Nov 9, 2018
Latest reply on Nov 12, 2018 by mnielsen-esristaff

I'm trying to access a secured ArcGIS Server MapService that uses AD authentication. I can access the service in the browser on the device with the credentials provided by the customer. Yet I'm unable to load a map as I'm always getting the following exception:

System.Net.Http.HttpRequestException: 401 (Unauthorized)
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () [0x0002a] in <f26106f7890945d7922088b4caacae4a>:0
at Esri.ArcGISRuntime.Internal.RequestRequiredHandler+<IssueRequestAndRespond>d__14.MoveNext () [0x005fd] in C:\daily_r\api_xam\dotnet\api\src\Esri.ArcGISRuntime\Esri.ArcGISRuntime.Shared\Internal\RequestRequiredHandler.cs:265
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <43dbbdc147f2482093d8409abb04c233>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <43dbbdc147f2482093d8409abb04c233>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <43dbbdc147f2482093d8409abb04c233>:0
at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0
at App4.MainPage+<InitMap>d__5.MoveNext () [0x00136] in C:\Users\<myuser>\source\repos\App4\App4\App4\MainPage.xaml.cs:56

The complete code is as follows:

using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Security;
using Esri.ArcGISRuntime.Xamarin.Forms;
using System;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
using Xamarin.Forms;

 

namespace App4
{
    public partial class MainPage : ContentPage
    {

        static string ServerUrl = "https://<url to customer's arcgis server>";
        static string layerUrl = "https://<url to customer's arcgis server>/rest/services/<service>/MapServer";
        static string user = "<user>";
        static string pw = "<password>";

 

        public MainPage()
        {
            InitializeComponent();

            InitMap();
        }

 

        private async Task InitMap()
        {
            try
            {

                var mapView = new MapView();

                var authManager = Esri.ArcGISRuntime.Security.AuthenticationManager.Current;
                authManager.AddCredential(new ArcGISNetworkCredential()

                {

                    Credentials = new NetworkCredential(user, pw),

                    ServiceUri = new Uri(layerUrl)

                });
                authManager.RegisterServer(new ServerInfo { ServerUri = new Uri(ServerUrl) });

                var map = new Map();
                map.Basemap.BaseLayers.Add(new ArcGISMapImageLayer(new Uri(layerUrl)));

                mapView.Map = map;

                Content = mapView;
                await map.LoadAsync();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                await DisplayAlert("Error", ex.ToString(), "OK");
            }
        }

    }

}

This happens with Runtime SDK version 100.2, 100.2.1, 100.3 and 100.4. The app itself is the default Xamarin.Forms template in Visual Studio 2017 using shared code. I'm testing on Android at the moment - can't say if it happens on all platforms.

The code for authentication is taken basically 1:1 from Use the Authentication Manager—ArcGIS Runtime SDK for .NET | ArcGIS for Developers (HTTP (integrated Windows authentication) authorization)

 

The server is version 10.3.1, using web authentication in IIS with AD accounts.

 

If needed I'm able to provide the URL to the service along with valid credentials.

Outcomes