Nishant Rana's Weblog


Home | Pages | Archives


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

January 20, 2009 3:39 PM


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.


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Posted by Nishant Rana

Categories: Microsoft Dynamics CRM

Tags:

7 Responses to “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. Love it, love it, love it !!!!!!!!!! Been trying for THREE days now, 3 dev days! Wow!!!!!!! thanks for this!

    Like

    By Stephen on May 11, 2009 at 3:54 PM

  2. Thanks for this!!! It saves my whole day :)..

    Like

    By Kishan on February 4, 2010 at 9:19 AM

  3. 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

    By Sanu on November 3, 2011 at 7:29 AM

    1. This was something i had done to connect to CRM Online for CRM 4.0, it might work with CRM 2011 as we are using the old end point. Not very sure about it though

      https://nishantrana.wordpress.com/2010/05/05/accessing-crmservice-live-or-online/

      Like

      By Nishant Rana on November 3, 2011 at 11:00 AM

      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

        By Sanu on November 3, 2011 at 12:42 PM

        1. Hello Sanu,
          Could you please send the code which you have been used for onpremise crm 4.0 as I am also getting the same error with 401 unauthorized error.

          Thanks,
          Jharana

          Like

          By Jharana Baliyar Singh on May 31, 2019 at 9:54 AM

  4. 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

    By Sreekanth on October 15, 2012 at 3:39 PM

Leave a Reply



Mobile Site | Full Site


Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.