HOW TO
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.
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)) { rolelist.Add(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": [ "admin", "publisher" ] } }
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(); envUID.setValue("{32d4c328-e473-4615-922c-63c108f55e60}"); 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."); } else { userInfoJSON.put("userName", userInfo.getName()); IEnumBSTR strEnum = userInfo.getRoles(); String roleName = strEnum.next(); StringBuffer rolesBuffer = new StringBuffer(); while(!roleName.isEmpty()) { 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"); }
Get help from ArcGIS experts
Download the Esri Support App