get username of geoprocessing service user

432
3
07-12-2021 02:29 AM
FlorianSteiner
New Contributor III

Hi there,

we are running several GP-services on ArcGIS Server, authentication is web-tier based on our companys Active Directory. I try to get the script to be aware of who started the service request (similar to editor tracking on feature classes). I found these two posts, where the conclusion is, that it's not easily possible:

https://community.esri.com/t5/python-questions/username-of-geoprocessing-service-user/td-p/491209

https://community.esri.com/t5/python-questions/is-there-any-way-to-get-the-username-of-user-logged-i...

Anything new on this topic or anyone else having the same problem? Can't be that hard for ESRI to implement, without me having to use SOIs, which I have no clue of.

Thanks and Best regards
- Florian

0 Kudos
3 Replies
nzjs
by
New Contributor III
0 Kudos
FlorianSteiner
New Contributor III

Yes, I saw that. But it looks like this file is only available when the service is run asynchronous. I also would need the jobID and I currently have no idea of how to get it within python.

0 Kudos
MatejVrtich
Esri Contributor

Hi,

It is already some time since this post, however I found the following is working (tested on AGE 10.8.1 base deployment with SAML auth):

 

import arcpy, json
from urllib import request, parse

tokenUrl = arcpy.GetParameterAsText(0)

def getUserInfo(url):
    try:
        token = arcpy.GetSigninToken()
        data = {"token": token["token"], "f": "json"}
        req = request.Request(
            url,
            data=parse.urlencode(data).encode(),
            headers={"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        )
        response = json.loads(request.urlopen(req).read().decode("utf-8"))
        return response["user"]
    except Exception as e:
        arcpy.AddError(e)

try:
    user = getUserInfo(tokenUrl)
    arcpy.AddMessage(user)
    arcpy.SetParameterAsText(1, user)
except:
    arcpy.AddMessage(arcpy.GetMessages())

 

The tokenUrl parametr is pointing to https://<gis_server_fqdn>/<web_adaptor/rest/self.

This parametr can be set as a constant during the publication of this python script as a GP web tool.

Point the tokenUrl parametr to https://<portal_fqdn>/<web_adaptor>/sharing/rest/portals/self to get more detailed user information.

Edit: if the GP service is running on ArcGIS Enterprise (base deployment), fetching the user info is a one liner

arcpy.GetPortalDescription(arcpy.GetActivePortalURL())["user"]

 

Matej