locked
Struggling to get the current users username RRS feed

  • Question

  • Hi,

    Im not a CRM expert but i need to integrate some software into it. I have installed Dynamics to test and set it up as an IFD. I have created a .aspx page and placed it in the ISV folder and linked it into my site map. I can sucessfully test integrate my software this way but i now need to get hold of the current users name to pass in.

    So if i use something like "User.Identity.Name" i can get the user name IF they are windows domain user. But if the user is signed in via forms authentication i get a really meaningless string.

    So i have dug around and found various post and the below code seems to be common, however when i run it all i get from the CRMService web service is a "HTTP status 401: Unauthorized".  Is there something else i need to do, or an easier way of doing this. All i need is to grab the username as a bit of text.

    Cheers,  JB

    	static
     string
     OrgName = "testOrg"
    ;
    	static
     string
     ServerName = "vm-dynamics"
    ;
    
    	Guid CurrentUser;
    
    	public
     CrmService GetCrmService()
    	{
    		CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, OrgName);
    
    		CrmService service = new
     CrmService();
    		service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    		CurrentUser = token.CallerId;
    
    		service.CrmAuthenticationTokenValue = token;
    		service.Url = string
    .Format("http://{0}/mscrmservices/2007/crmservice.asmx"
    , ServerName);
    
    		return
     service;
    	}
    
    	protected
     override
     void
     OnLoad(EventArgs e)
    	{
    		using
     (new
     CrmImpersonator())
    		{
    				//Create the Service
    
    				CrmService crmService = GetCrmService();
    				RetrieveRequest rr = new
     RetrieveRequest();
    				ColumnSet cset = new
     ColumnSet(EntityName.systemuser.ToString());
    				cset.AddColumn("AttributeName"
    );
    				cset.AddColumn("domainname"
    );
    				rr.ColumnSet = cset;
    				rr.ReturnDynamicEntities = true
    ;
    				TargetRetrieveDynamic trd = new
     TargetRetrieveDynamic();
    				trd.EntityId = CurrentUser;
    				trd.EntityName = EntityName.systemuser.ToString();
    				rr.Target = trd;
    				DynamicEntity user = (DynamicEntity)((RetrieveResponse)crmService.Execute(rr)).BusinessEntity;
    				// Do Stuff
    
    		}
    	}
    
    Thursday, September 24, 2009 8:25 AM

Answers

  • Its simple Bickley. Follow steps below:

    1. Add MS CRM 4 web reference i.e. http://CRMServer:5555/MSCrmServices/2007/CrmService.asmx
    2. add using solutionname.webservicename in imported directories ... for example
                   using System.Net;
                   using Create_Invoice_Product.CrmWsdl;
    3. use code below:

                string orgName = "myOrg";
                string wUrl = "http://CRMServer:5555/MSCrmServices/2007/CrmService.asmx ";
                CrmService ser = new CrmService();
                ser.Url = wUrl;
                CrmAuthenticationToken myToken = new CrmAuthenticationToken();
                myToken.AuthenticationType = 0;
                myToken.OrganizationName = orgName.ToString();
                ser.CrmAuthenticationTokenValue = myToken;
                ser.Credentials = System.Net.CredentialCache.DefaultCredentials;

                WhoAmIRequest whoRequest = new WhoAmIRequest();
                WhoAmIResponse whoResponse = (WhoAmIResponse)ser.Execute(whoRequest);

    4. whoResponse object contains your loggedIn user info :)


    Sohaib Ahmad [Business Management Solution Professional] http://www.solutiontalk.blogspot.com

    This will work for on-premice deployment, but not for IFD. I suggest you to read questions more attentively.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    • Marked as answer by Jim Glass Jr Thursday, September 24, 2009 6:45 PM
    Thursday, September 24, 2009 10:43 AM
    Moderator

All replies

  • Hi.

    Check this article about Authentication in IFD mode.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Thursday, September 24, 2009 8:30 AM
    Moderator

  • Hi Thanks for the response.

    However, the article is about authenticating a user. I dont want to authenticate them, i just want to know who they are, they are already logged in?

    Joe
    Thursday, September 24, 2009 8:53 AM
  • Hi, Joe.

    Check this thread .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Thursday, September 24, 2009 9:02 AM
    Moderator
  • Its simple Bickley. Follow steps below:

    1. Add MS CRM 4 web reference i.e. http://CRMServer:5555/MSCrmServices/2007/CrmService.asmx
    2. add using solutionname.webservicename in imported directories ... for example
                   using System.Net;
                   using Create_Invoice_Product.CrmWsdl;
    3. use code below:

                string orgName = "myOrg";
                string wUrl = "http://CRMServer:5555/MSCrmServices/2007/CrmService.asmx";
                CrmService ser = new CrmService();
                ser.Url = wUrl;
                CrmAuthenticationToken myToken = new CrmAuthenticationToken();
                myToken.AuthenticationType = 0;
                myToken.OrganizationName = orgName.ToString();
                ser.CrmAuthenticationTokenValue = myToken;
                ser.Credentials = System.Net.CredentialCache.DefaultCredentials;

                WhoAmIRequest whoRequest = new WhoAmIRequest();
                WhoAmIResponse whoResponse = (WhoAmIResponse)ser.Execute(whoRequest);

    4. whoResponse object contains your loggedIn user info :)


    Sohaib Ahmad [Business Management Solution Professional] http://www.solutiontalk.blogspot.com
    Thursday, September 24, 2009 10:17 AM
  • Its simple Bickley. Follow steps below:

    1. Add MS CRM 4 web reference i.e. http://CRMServer:5555/MSCrmServices/2007/CrmService.asmx
    2. add using solutionname.webservicename in imported directories ... for example
                   using System.Net;
                   using Create_Invoice_Product.CrmWsdl;
    3. use code below:

                string orgName = "myOrg";
                string wUrl = "http://CRMServer:5555/MSCrmServices/2007/CrmService.asmx ";
                CrmService ser = new CrmService();
                ser.Url = wUrl;
                CrmAuthenticationToken myToken = new CrmAuthenticationToken();
                myToken.AuthenticationType = 0;
                myToken.OrganizationName = orgName.ToString();
                ser.CrmAuthenticationTokenValue = myToken;
                ser.Credentials = System.Net.CredentialCache.DefaultCredentials;

                WhoAmIRequest whoRequest = new WhoAmIRequest();
                WhoAmIResponse whoResponse = (WhoAmIResponse)ser.Execute(whoRequest);

    4. whoResponse object contains your loggedIn user info :)


    Sohaib Ahmad [Business Management Solution Professional] http://www.solutiontalk.blogspot.com

    This will work for on-premice deployment, but not for IFD. I suggest you to read questions more attentively.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    • Marked as answer by Jim Glass Jr Thursday, September 24, 2009 6:45 PM
    Thursday, September 24, 2009 10:43 AM
    Moderator
  • ok, my mistake :)
    Sohaib Ahmad [Business Management Solution Professional] http://www.solutiontalk.blogspot.com
    Thursday, September 24, 2009 11:06 AM