DefaultCredentials in Accessing CRM / Sharepoint Web Services

asp.netcrmsharepointweb-services

I made an application that access CRM's web service. The problem is, when I deployed the dll into Sharepoint server, it returned error 401 unauthorized. Apparently the System.Net.CredentialCache.DefaultCredentials didn't work (my suspicion). Here's the code.

CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.AD;
token.OrganizationName = ORGANIZATION_NAME;

CrmService service = new CrmService();
service.Url = "http://crmserver:5555/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.PreAuthenticate = true;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

It goes vice-versa.

When I made application that access Sharepoint's webservice (coding the plugin) and deployed it to CRM server. It couldn't access the Sharepoint's web service. Unauthorized error. Here is the code:

Lists listService = new Lists();
listService.PreAuthenticate = true;
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = "http://sharepointserver/webname/_vti_bin/Lists.asmx";

My CRM server and Sharepoint server are in the same domain.

For both code, if I changed the credentials part into something like this then deploy it on server, it can run.

service.Credentials = new NetworkCredential("username", "password", "domain");

Still, I don't want to do this because it reveals user's password in the code. May anyone help me?

The IIS in both server doesn't allow Anonymous Access and it uses Integrated Windows Authentication.

Thank you


From my local computer, I can access the CRM web services or Sharepoint web services. I guess I'm authorized because the DefaultCredentials sent my credentials that its password is saved in the "Stored Username and Password" (Control Panel > User Accounts > tab Advanced > Manage Passwords)
This way, I don't have to type:

service.Credentials = new NetworkCredential("username", "password", "domain");

and my DefaultCredentials from my local comp is authorized to access the web services.

I tried to implement this on the Sharepoint server that access CRM web services. and..tadaa..it won't work. hahaha..

can we inject credentials to DefaultCredentials in server?

the last thing I want to do is to hardcode the useraccount (like the code above)

Best Solution

Could be that you need to be running Kerberos for authentication, but cannot be sure and it is a pain to setup just to check.