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);
});