OAuth2 authentication challenge

4450
7
06-03-2015 06:15 PM
ChristosStamatopoulos
New Contributor II

Hi all,

I am attempting to switch my standard app licensing from Token to OAuth2.

Things is it is not challenging the user for credential and instead throws an Exception.

Esri.ArcGISRuntime.Http.ArcGISWebException: Error code '400' : 'Unable to generate token.'

'username' must be specified.

'password' must be specified.

I should say that It works fine with the token.

Here is what I do.

First set up our challenge handler

            // setup our sign challenge handler

            var signInChallengeHandler = new Esri.ArcGISRuntime.Toolkit.Security.SignInChallengeHandler()

            {

                AllowSaveCredentials = true,

                CredentialSaveOption = Esri.ArcGISRuntime.Toolkit.Controls.CredentialSaveOption.Unselected

            };

            Esri.ArcGISRuntime.Security.IdentityManager.Current.ChallengeHandler = signInChallengeHandler;

            Esri.ArcGISRuntime.Security.IdentityManager.Current.OAuthAuthorizeHandler =

                new Esri.ArcGISRuntime.Toolkit.Security.OAuthAuthorizeHandler();

          // code for basic license....

Second register the server

            var serverInfo = new ServerInfo()

            {

                ServerUri = portalServerUrl,

                OAuthClientInfo = new OAuthClientInfo()

                {

                    ClientId = "xxxxxx",

                    ClientSecret = "xxxxxxxxxx"

                },

                TokenAuthenticationType = TokenAuthenticationType.OAuthAuthorizationCode

            };

            Esri.ArcGISRuntime.Security.IdentityManager.Current.RegisterServer(serverInfo);

Finally request for the credentials

                var credRequest = new CredentialRequestInfo()

                {

                    ServiceUri = portalServerUrl,

                };

                var cred = await IdentityManager.Current.GetCredentialAsync(credRequest, true);

If instead I use

                  var cred = await IdentityManager.Current.GenerateCredentialAsync(portalServerUrl, "user", "pass");

I get Esri.ArcGISRuntime.Http.ArcGISWebException: Error code '400' : 'client_id not specified'

What am I missing here ?

Tags (2)
0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor

var cred = await IdentityManager.Current.GetCredentialAsync(credRequest, true);

This should work. Does the OAuth authorization page show up?

var cred = await IdentityManager.Current.GenerateCredentialAsync(portalServerUrl, "user", "pass");

AutorizationCode OAuth workflow doesn't support setting the user and password by the API. Password and access authorization must be explicitly validated by the user.

Though you can use GenerateCredentialAsync without specifying the user/password.

var cred = await IdentityManager.Current.GenerateCredentialAsync(portalServerUrl);

In this case, you should see the authorization page.

0 Kudos
ChristosStamatopoulos
New Contributor II

No that is the problem,

The OAuth authorization page does not show up at all and I just get the exception.

If I use what you said

var cred = await IdentityManager.Current.GenerateCredentialAsync(portalServerUrl);

I get

Esri.ArcGISRuntime.Http.ArcGISWebException: Error code '400' : 'Unable to generate token.'

'username' must be specified.

'password' must be specified.

0 Kudos
DominiqueBroux
Esri Frequent Contributor

Actually it's likely because you didn't set the RedirectUri (sorry I should have noticed that earlier)

Try

                OAuthClientInfo = new OAuthClientInfo()

                {

                    ClientId = "xxxxxx",

                    RedirectUri = "urn:ietf:wg:oauth:2.0:oob"

                },

Note: ClientSecret is optional

0 Kudos
ChristosStamatopoulos
New Contributor II

It is still not showing the authentication page. Same exception.

0 Kudos
DominiqueBroux
Esri Frequent Contributor

Strange. The behavior is as if the server was not registered to use the OAuthAuthorizationCode workflow.

Could you post a simple repro case. I'll test my side.

Thanks.

0 Kudos
ChristosStamatopoulos
New Contributor II

Thank you,

Can I get an email address to sent you the repro case ?

0 Kudos
DominiqueBroux
Esri Frequent Contributor
0 Kudos