Using IdentityManager to generate a short lived token

3480
5
11-14-2013 12:31 PM
DaveTimmins
Occasional Contributor II
Hi,

just thought I'd post a quick note about generating tokens with the IdentityManager.

The IdentityManager tries to determine the url to use for generating a token based on the url you supply and this works so long as you have /rest/services or /sharing in your url

var credential = await IdentityManager.Current.GenerateCredentialTaskAsync(@"http://serverapps10.esri.com/arcgis/rest/services", "user1", "pass.word1");


However if you just try creating one for the server url like http://serverapps10.esri.com/arcgis it will fail. You can get around this by registering the server first though

var serverInfo = new ESRI.ArcGIS.Client.IdentityManager.ServerInfo { ServerUrl = @"http://serverapps10.esri.com/arcgis" };
            IdentityManager.Current.RegisterServers(new[] { serverInfo });
      
            var credential = await IdentityManager.Current.GenerateCredentialTaskAsync(@"http://serverapps10.esri.com/arcgis", "user1", "pass.word1");
0 Kudos
5 Replies
DominiqueBroux
Esri Frequent Contributor
Hi Dave,

Your are right. Thanks for sharing your tip.

Note however that using such Url may be ambiguous because we might have a portal under "http://<domain>/arcgis/sharing" and a hosted ArcGIS server under "http://<domain>/arcgis/rest/services".
Both servers don't share the same token generation workflow.

So I would recommend to keep your first version of code which works without any registration:

var credential = await IdentityManager.Current.GenerateCredentialTaskAsync(@"http://serverapps10.esri.com/arcgis/rest/services", "user1", "pass.word1");


/Dominique
0 Kudos
QuinnKorbulic1
New Contributor III
Hello,

Should await --> async task, e.g.

var credential = await IdentityManager.Current.GenerateCredentialTaskAsync("url", "user1", "pass.word1");


work in silverlight? When I try I get the message:

"The await operator can only be used within an async method. Consider making this method with the 'async' modifier and changing its return type to 'Task'."

Thanks.
0 Kudos
RichardWatson
Frequent Contributor
In order to await the calling function must be declared as async.

Google on async/await.  This is not Silverlight specific behavior but rather the way that the language works.
0 Kudos
nakulmanocha
Esri Regular Contributor
Hello,

Should await --> async task, e.g.

var credential = await IdentityManager.Current.GenerateCredentialTaskAsync("url", "user1", "pass.word1");


work in silverlight? When I try I get the message:

"The await operator can only be used within an async method. Consider making this method with the 'async' modifier and changing its return type to 'Task'."

Thanks.


.NET 4.5 provides better asynchronous programming  techniques using AsyncTask API operations to handle multiple requests asynchronously without blocking the UI. All AsyncTask are awaitable and hence require operations to be async with return type as Task instead of void. You could also use Task.Run to await a Task within any synchronous method if you would like. More about parallel/async programming can be found here
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Hello,

Should await --> async task, e.g.

var credential = await IdentityManager.Current.GenerateCredentialTaskAsync("url", "user1", "pass.word1");


work in silverlight? When I try I get the message:

"The await operator can only be used within an async method. Consider making this method with the 'async' modifier and changing its return type to 'Task'."

Thanks.


You can use async/await c# keywords in Silverlight but you need to load and reference the Async package http://www.nuget.org/packages/Microsoft.Bcl.Async/1.0.16

Note that for simplicity  I used 'await' in my sample but you could as well used the event based method GenerateCredentialAsync.
0 Kudos