Simple question:
How do I display a secured feature layer on a map in an ArcGIS Runtime for WPF app?
I do not want to query the user for username/password. I just want to hardcode it.
I know that I need to use IdentityManager and I have successfully done precisely the above with ArcGIS Server, but for some reason it is not working for me with ArcGIS Online.
This is what I do for ArcGIS Server. I call this function before I use the secured service (it is a GP-service):
private async Task GenerateToken(string tokenUrl, string username, string password)
{
IdentityManager.Current.DefaultReferer = "BlahBlah";
IdentityManager.Current.ChallengeMethodEx += SignInDialog.DoSignInEx; // I don't think that this has any effect.
IdentityManager.Credential crd = await IdentityManager.Current.GenerateCredentialTaskAsync(tokenUrl, username, password);
if (crd != null)
IdentityManager.Current.AddCredential(crd);
else
throw new Exception("Unknown error");
}
I think that at least two things are different with ArcGIS Online:
- I do not know what to use for the tokenUrl.
- I would prefer to somehowget this taken care of during application startup. I want the layer to display as soon as the application is loaded.
- I do not know what to use for the tokenUrl.
You don't have to know the token url, the first url parameter of GenerateCredentialTaskAsync is the ArcGIS secured resource you want to access to. The token wil be discovered automatically internally.
I would prefer to somehowget this taken care of during application startup. I want the layer to display as soon as the application is loaded.
Yes. That's possible.
You have actually 2 options:
1) Option 1: Generate a token when an authorization error is intercepted by the IdentityManager (i.e at runtime when trying to access to the secured services)
To do that you have to define a challenge method that will handler the error and can, for example, generate a token with an hardcoded username/password.
As, in this case ,the challenge method is just a call to GenerateCredentialAsync, the IM initialization can be as simple as:
IdentityManager.Current.ChallengeMethod += (url, handler, options) => IdentityManager.Current.GenerateCredentialAsync(url, "<username>", "<password>", handler, options);
2) Option 2: At startup, generate a token and add it to IM which will use it when trying to access to the service
Something like:
// generate a token
var crd = await Client.IdentityManager.Current.GenerateCredentialTaskAsync(<resource url>, <username>, <password>);
// add the token to IM
Client.IdentityManager.Current.AddCredential(crd);
// activate IM without challenge (or set a challenge method for managing access to secure services not declared at startup)
Client.IdentityManager.Current.Enabled = true;