Error: The request failed with HTTP status 401 or Error 401.2: Unauthorized while using CrmService in ASP.NET or windows application CRM 4.0

To use CrmService or Metadata Service (CRM 4.0) within an ASP.NET page or a windows application we need to make use of CRM Authentication token.If we are using Active Directory Authentication this is the code for that

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.OrganizationName = “organizationName”;

//0 – AD

//1 – Passport

//2 – Form Authentication

token.AuthenticationType = 0;

CrmService service = new CrmService();

service.Credentials =System.Net.CredentialCache.DefaultCredentials;

service.CrmAuthenticationTokenValue = token;

service.Url = http://servername:port/mscrmservices/2007/crmservice.asmx”;

try{

WhoAmIRequest myReq = new CrmSdk.WhoAmIRequest();

WhoAmIResponse myResp = (CrmSdk.WhoAmIResponse)service.Execute(myReq);

}

catch (SoapException ex){

Response.Write(ex.Detail.InnerText);

}

catch (Exception ex){

Response.Write(ex.Message);}

 

If we get the above unauthorized access error it could be because either there is problem in our CrmAuthenticationToken, may be we could have assigned wrong organization name or authentication type. If we are using CrmService in an ASP.NET page than we need to use impersonation.

 

<authentication mode=Windows/>

<identity impersonate=true/>

 

If we are using 2006 end point of CrmService we don’t have to use CrmAuthenticationToken.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

6 thoughts on “Error: The request failed with HTTP status 401 or Error 401.2: Unauthorized while using CrmService in ASP.NET or windows application CRM 4.0”

  1. Hey Thanks Nishant,this post solved my problem. I had spelling mistake in my Organization name(silly mistake but consumed half of my day) .

    Like

  2. Hi I am trying to access the 2007 endpoint of Online MS CRM 2011. Its a trial version right now.

    Under the developer resources, i could get the organization unique Name.
    And used that in
    token.OrganizationName = “1aff7162df064dec9dded680f2405c3e”;

    What so ever , i tried 0, 1 and 2 as the authentication type.
    Still i get an 401 unauthorized error.

    Any idea on using the 2007 endpoint of online MS CRM Online?

    Otherwise, for an on premise version, my code works fine on crm 4,0.

    Any suggestion help would be of great help for me !

    Like

      1. Thank you Nishant for the quick response!

        In fact, i tried doing http://msdn.microsoft.com/en-us/library/bb955360.aspx
        exactly what they were saying.

        There they say to add CrmDiscoveryService.asmx. ( while in crm2011 , all i see is Discovery.svc).
        In fact when i download and add that , it says :

        The document was understood, but it could not be processed.
        – The WSDL document contains links that could not be resolved.
        – There was an error downloading ‘http://dev.crm5.dynamics.com/XrmServices/2011/Discovery.svc?wsdl=wsdl1’.
        – Unable to connect to the remote server

        And unable to add the proxy.

        Any idea why it may be so? Is it because its trial version? Will that matter ?

        Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s