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.

Love it, love it, love it !!!!!!!!!! Been trying for THREE days now, 3 dev days! Wow!!!!!!! thanks for this!
LikeLike
Thanks for this!!! It saves my whole day :)..
LikeLike
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 !
LikeLike
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/
LikeLike
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 ?
LikeLike
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
LikeLike
Hey Thanks Nishant,this post solved my problem. I had spelling mistake in my Organization name(silly mistake but consumed half of my day) .
LikeLike