0
votes

What is the recommended approach for obtaining the identity of the currently logged in MS Dynamics user from code behind for a custom ASPX page that lives in the ISV directory?

The approach for doing this must be agnostic of whether Dynamics is using AD/NTLM authentication or other authentication mechanisms and must not require enabling impersonation or changing the web.config.

Thanks

2

2 Answers

1
votes

You can get the systemuserid from the CRM database by executing a WhoAmIRequest through the CRM web service and then get the systemuser record via crmservice.Retrieve(). As long as the user has any CRM security roles assigned at all, WhoAmIRequest and read access to their own systemuser record should always work.

0
votes

using the Xrm Context with the SDK

 public Xrm.systemuser CurrentUser   {
     get
     {
        var context = new XrmDataContext();

        var reponse = context.UsingService(
               service => (WhoAmIResponse)service.Execute(new WhoAmIRequest()));


        return (from user in context.systemusers
                where user.systemuserid == reponse.UserId
                select user).Single();
     }   }

or with the web services (called 'crm' here)

  public systemuser CurrentUser
  {
     get
     {
        WhoAmIRequest userRequest = new WhoAmIRequest();
        WhoAmIResponse current = (WhoAmIResponse)crm.Execute(userRequest);
        return (systemuser)crm.Retrieve(EntityName.systemuser.ToString(), current.UserId, new AllColumns());

     }
  }