Using Identity Manager to Generate Tokens - JavaScript API

Blog Post created by mlewis22 on May 27, 2015


Whilst trawling through various posts I have noticed that a lot of application designs utilise the ArcGIS for Server Token authentication method. In many cases developers use a simple JQuery Ajax request to the generate token REST process. Whilst there is nothing wrong with approach I wanted to highlight an alternative method using the Identity Manager. I will briefly describe what identity manager is and then move on to a simple sample to get you going.




What is Identity Manager?

Put simply, its a module that provides a framework and helper methods for managing user credentials for both ArcGIS for Server resources & ArcGIS Online resources. The identity manager also supports CORS (cross origin resource sharing) so long as your browser is CORS enabled. You can find more details on the class here.



Generating a Token

once you have set up your HTML page you will need to generate a function using dojo require.


require(["esri/ServerInfo", "esri/IdentityManager", "esri/config", "dojo/_base/lang"
], function (ServerInfo, esriId, esriConfig, lang) { })


I've set up a basic template on JSFiddle - Edit fiddle - JSFiddle


The identiy manager has a method called generateToken(serverInfo, userInfo, options?) .


The method requires Three inputs:


  1. serverInfo - A ServerInfo object that contains a token service URL.
  2. userInfo - An object containing username and password
  3. options - Other extra objects that may be required.



You may need to push your server into the CORS exception."addserverhere");



serverInfo Object

The first parameter is a serverInfo Object. This is a class within the ESRI JS Api -


We simply need to build the Object using this class and add it to the generateToken method.


var idBase = new esri.IdentityManagerBase();
var serverInfo = {
     "server": hostname,
     "tokenServiceUrl": hostname + "/arcgis/tokens/",
     "shortLivedTokenValidity": 1,
     "currentVersion": 10.31,//update necessary
     "hasServer": true
var def = idBase.generateToken(serverInfo.....)



hostname in  this case is your server URL. This could be localhost (http://localhost:6080) or  a domain (


userInfo Object

Next generate the user object like so..


var def = idBase.generateToken(serverInfo, { "username": "addyourusername", "password": "addyourpassword" },.....)



there is only one extra option for this method - isAdmin. by default this is set to false. However for clarity im going to add it to the method.


 var def = idBase.generateToken(serverInfo, { "username": "addyourusername", "password": "addyourpassword" }, { isAdmin: false });


Create a Callback

create a call back so you can manage the token once its returned from the server. Lang.Hitch is bringing the callback into scope.


def.addCallback(lang.hitch(this, function (tokenInfo) {
     document.getElementById("input").value =tokenInfo.token



This snippet adds the token to an input field.




You can view a full working example here - Edit fiddle - JSFiddle