IdentityManager Question

2210
1
06-24-2013 07:35 AM
Labels (1)
BrianRassier
Occasional Contributor
To programmatically generate a token, it seems that we must set the IdentityManager.Current.ChallengeMethod to the proper method which can give user/pw, and then call GenerateCredentialAsync from within the challenge method. That method then also raises callback actions that were passed to it, and uses options that were passed to it. This can be seen in the SDK Sample: Datasources->Web Maps->Load Secure WebMap.

My question is, what functionality in the SDK will actually trigger the challenge method being called? It seems to be triggered when actually using a service that needs a token, via certain SDK objects (e.g. document.GetMapAsync). We want to programmatically generate this token when starting our application, and not wait for an unknown SDK object to trigger the call of the challenge method. Is this possible?
0 Kudos
1 Reply
DominiqueBroux
Esri Frequent Contributor
To programmatically generate a token, it seems that we must set the IdentityManager.Current.ChallengeMethod to the proper method which can give user/pw, and then call GenerateCredentialAsync from within the challenge method.


You can also call GenerateCredentialAsync  out of the challenge method and then call AddCredential to register this credential with the IdentityManager. Once done, the IdentityManager will automatically use this credential to access to the specified arcgis resource (portal or server).

Example of code snippet:

private Task InitIdentityManager()
{
    var tcs = new TaskCompletionSource<bool>();
    IdentityManager.Current.TokenGenerationReferer = "arcgis.com";
    IdentityManager.Current.ChallengeMethodEx += SignInDialog.DoSignInEx; // activate the IM with the standard SignInDialog

    IdentityManager.Current.GenerateCredentialAsync("http://www.arcgis.com/sharing/rest", "username", "password", (crd, error) =>
    {
        if (crd != null)
        {
            IdentityManager.Current.AddCredential(crd);
            tcs.SetResult(true);
        }
        else
            tcs.SetException(error ?? new Exception("Unknown error"));
    });
    return tcs.Task;
}


Instead of using GenerateCredentialAsync, you can also call GetCredentialAsync in order to challenge the user using the toolkit signInDialog.

Something like:
IdentityManager.Current.GetCredentialAsync("http://www.arcgis.com/sharing/rest", true, (crd, error) =>
{
    if (crd != null)
         IdentityManager.Current.AddCredential(crd);
});
0 Kudos