ArcGIS Token not working properly when creating programmatically

436
2
01-16-2018 05:46 AM
ShaikhRizuan
New Contributor III

Hi,

 I am able to create ArcGIS Token for Secured Map Services using below code:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public string createToken()
{

string username = "siteadmin";
string password = "password";
string httpreferer = "http://MachineName/Test.html";
string generateTokenUrl = "https://MachineName:6443/arcgis/tokens/generateToken";
int exp = 60;


if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
TokenModel tokenModel = new TokenModel(username, password, httpreferer, exp);

string post = tokenModel.GetQueryStringParameter();
WebClient clientToken = new WebClient();
clientToken.Headers.Add("Content-Type: application/x-www-form-urlencoded");
string tokenResult = clientToken.UploadString(generateTokenUrl, post);
}

return tokenResult ;

}

public string GetQueryStringParameter()
{
return "f=" + this.f + "&username=" + this.username + "&password=" + this.password + "&referer=" + this.httpreferer + "&expiration=" + this.expiration;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The token generated from the above code is like this : jRI_CQUsuxytOHJaXAxqLyHL9N1hgBDbM8RtNiJLbLM. The above is token is not respecting the above httpreferer URL. If i am adding the above token to the same map service for different site, its working. PFB Image for the same

Correct Referer URL

Test.html

Incorrect Referer URL

Token

But when i manually created the token (from https://MachineName:6443/arcgis/admin/generateToken  with the above referer (i.e.  http://MachineName/Test.html) , I am getting token like this: WX8Fa7HdCJzgpJyTun8B5_lvoaJP2PzdkMxpAeW7phmGNd0aANCq5FSmetfI7n0tmy4CD7uo58ZA8gqRQouTYGLe5p4db0gs-Q_BNIjWT89ZjeUxxCj5e8dQpAd9xukX

Which is quite big and working as expected (i.e. it is not allowing to access map service from any other http referer URL. One pop-up generated asking for user name & password.

So, I have change the "generateTokenUrl" parameter to https://MachineName:6443/arcgis/admin/generateToken and i got the token as expected (i.e. quite big as above). But it is not working for any application (i.e. neither for the http referer URL nor any other application).A pop-up generated asking for user name & password. PFB Image

Test HTML

How to resolve this?

0 Kudos
2 Replies
EvelynHernandez
Regular Contributor

U need to use the token per each layer u want to add in the map. If u dont do that, it will pop up the login window.

0 Kudos
ShaikhRizuan
New Contributor III

There is only one layer in the map service.

0 Kudos