AnsweredAssumed Answered

StackOverflowException accessing secure service

Question asked by alehmann on Oct 2, 2018
Latest reply on Oct 4, 2018 by alehmann

When using the below code to access a secured service, I more or less immediately get a `StackOverflowException`. Because we don't want to challenge our users when accessing this service, we prepare a valid credential somewhere else in the app and provide it via `CreateTokenCredential` which I simplified here for demonstration purposes.

Am I doing something wrong here or is this a bug in the SDK?

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            InitMap();
        }

        private async Task InitMap()
        {
            try
            {
                var mapView = new MapView();
                var authManager = Esri.ArcGISRuntime.Security.AuthenticationManager.Current;
                var ServerUrl = "https://example.com/arcgisweb";
                var layerUrl = "https://example.com/arcgisweb/rest/services/AppService/MapServer";

                authManager.ChallengeHandler = new ChallengeHandler((requestInfo) => Task.FromResult(CreateTokenCredential()));
                authManager.RegisterServer(new ServerInfo { ServerUri = new Uri(ServerUrl) });

                var tokenCredential = await authManager.GetCredentialAsync(new CredentialRequestInfo
                {
                    AuthenticationType = AuthenticationType.Token,
                    ServiceUri = new Uri(ServerUrl)
                }, false);
                authManager.AddCredential(tokenCredential);

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

                mapView.Map = map;

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

        private static Credential CreateTokenCredential()
        {
            var token = "<pre-generated token>";

            var cred = new ArcGISTokenCredential();
            cred.UserName = "<valid user>";
            cred.Password = "<valid password>";
            cred.Token = token;

            return cred;
        }
    }

 

When I remove all the parts with custom authentication, it works as expected (at least on UWP) as in I'm challenged for username and password and the map loads correctly.

Outcomes