How to get accessTokenData of the logged in user (not application pool account) with Core Service ?

Below line retrieves the application pool account, instead of the logged in user.

AccessTokenData currentUser = tridionClient.ClientInstance.GetCurrentUser();

where tridionClient is an instance of SessionAwareCoreServiceClient

//gets current logged in username

string currentUserName = System.Web.HttpContext.Current.User.Identity.Name;

// Question : Below code retrieves current user. How do I retrieve the AccessTokenData of the retrieved user?

IdentifiableObjectData[] allUsers = tridionClient.ClientInstance.GetSystemWideList(new UsersFilterData { NameSearchMode = StringSearchMode.ExactMatch , Name = currentUserName, IsPredefined = false, BaseColumns = ListBaseColumns.Id });  

foreach (IdentifiableObjectData user in allUsers)
{
    UserData thisUser = (UserData)tridionClient.ClientInstance.Read(checkUser.Id, readOptions);

    if (thisUser.Title == currentUserName)
    {
        currentUser = thisUser;
    }
}
  • Your question isn't quite clear, because you already answered it yourself. The Core Service API holds the following method: AccessTokenData ICoreService201603.GetCurrentUser ![enter image description here](i.stack.imgur.com/pAyai.png) – Atila Sos Sep 10 at 8:38
  • Please clarify your question. It is not clear what is going wrong and what you are expecting. The type of the ClientInstance property is also not clear -- is that a SessionAwareCoreService instance? – Peter Kjaer Sep 10 at 13:45
  • I have modified the question. – Deepthi Mandava Sep 10 at 14:14
up vote 3 down vote accepted

As Atila mentioned, the method to get the Access Token of the “current user” is the method which you already found: GetCurrentUser.

The confusion here seems to be in what “current user” means. This may be the authenticated user/identity or an impersonated user, as mentioned in the Remarks section in the docs.

If you are creating a Core Service client without explicitly specifying credentials, Windows authentication will implictly use the identity of the thread/process where the client is created. In case you are creating a Core Service client in a Web Application, this is typically the “Application Pool Identity” (rather than the user performing the request). If this identity has been configured to have access to Tridion, the “current user” for the Core Service Session will be this Application Pool Identity.

Typically you want the remote user to be the current user for the Core Service Session. This can be achieved by impersonating that remote user using the Core Service’s Impersonate method. Of course, not everybody is allowed to impersonate other users, but the Application Pool Identity is typically configured as impersonation user in Tridion, so that the Impersonate method can be used if you are creating a Core Service client in a Web Application.

Note that you need a SessionAwareCoreServiceClient for this purpose; this gives you a Core Service session which lasts across requests (for the lifetime of the client). You will see that after you called the Impersonate method, GetCurrentUser will return the Access Token of the impersonated user.

Long story short: try this:

coreServiceClient.Impersonate(HttpContext.Current.User.Identity.Name);
AccessTokenData remoteUserAccessToken = coreServiceClient.GetCurrentUser();
  • Thanks for the answer. I was trying to look for a solution where we can get the user without setting up the impersonation user as suggested in your solution. – Deepthi Mandava Sep 10 at 20:23

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.