How To: Get the current user name and roles using a server object extension


This article provides code samples for .NET and Java REST server object extensions (SOEs), showing how the user name and roles for an SOE user accessing a secured ArcGIS Server resource can be obtained.


The first section below shows the code snippet in C# for a .NET SOE, and the second section provides sample code written in Java. Depending upon the language of choice, follow either solution.

  • Code snippet (C#)

    First, obtain a pointer to the IServerEnvironment interface. It has a UserInfo property of IServerUserInfo type. From that property, the user name and roles can be obtained.
            public IServerEnvironment3 GetServerEnvironment()
                UID uid = new UIDClass();
                uid.Value = "{32D4C328-E473-4615-922C-63C108F55E60}";
                //use activator to cocreate singleton
                Type t = Type.GetTypeFromProgID("esriSystem.EnvironmentManager");
                System.Object obj = Activator.CreateInstance(t);
                IEnvironmentManager environmentManager = obj as IEnvironmentManager;
                return (environmentManager.GetEnvironment(uid) as IServerEnvironment3);

    Use the IServerEnvironment to get the user information. Below is a sample of a REST resource handler method called AdminInfo. This method can be used exactly like other methods in the SOE examples provided in the .NET ArcObjects SDK.

    private byte[] AdminInfo(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties) { 
    responseProperties = null;
     IServerEnvironment3 senv = GetServerEnvironment() as IServerEnvironment3; 
    JsonObject result = new JsonObject(); 
    JsonObject suinfoj = new JsonObject();
     //get user info and serialize into JSON 
    IServerUserInfo suinfo = senv.UserInfo;
     if (null != suinfo) { 
    suinfoj.AddString("currentUser", suinfo.Name);
     IEnumBSTR roles = suinfo.Roles; 
    List<string> rolelist = new List<string>(); 
    if (null != roles) { 
    string role = roles.Next(); 
    while (!string.IsNullOrEmpty(role)) {
     role = roles.Next(); 
    } } 
    suinfoj.AddArray("roles", rolelist.ToArray());
     result.AddJsonObject("serverUserInfo", suinfoj); 
    } else { result.AddJsonObject("serverUserInfo", null); }
     return Encoding.UTF8.GetBytes(result.ToJson()); 

    This code returns the following JSON:

     "serverUserInfo": {
      "currentUser": "demo",
      "roles": [
  • Code snippet (Java)

    If a REST SOE has a subresource called UserCredentials, it could have the following implementation in Java:

    private byte[] getSubresourceUserCredentials() throws Exception {
    	EnvironmentManager envMgr = new EnvironmentManager();
    	UID envUID = new UID();
    	Object envObj = envMgr.getEnvironment(envUID);
    	JSONObject userInfoJSON = new JSONObject();
    	try {
    	    IServerEnvironment2 env = new IServerEnvironment2Proxy(envObj);
                //get user info and serialize into JSON
    	    IServerUserInfo userInfo = env.getUserInfo();
    	    if(userInfo == null)
    		userInfoJSON.put("error", "UserInfo is null. No user/roles configured.");
    		userInfoJSON.put("userName", userInfo.getName());
    		IEnumBSTR strEnum = userInfo.getRoles();
    		String roleName = strEnum.next();
    		StringBuffer rolesBuffer = new StringBuffer();
    		    rolesBuffer.append(roleName + ";");		    
    		    roleName = strEnum.next();
    		userInfoJSON.put("roles", rolesBuffer.toString());
    	} catch (Exception e) {
    	    this.serverLog.addMessage(3, 200, "Exception: " + e.getMessage());
    	return userInfoJSON.toString().getBytes("utf-8");