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.
