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.


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

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

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

    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

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

Leave a reply to Nishant Rana Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.