title: DefaultCredentials in Accessing CRM / Sharepoint Web Services
body: <p>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.</p>
  
  <pre><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;
  </code></pre>
  
  <p>It goes vice-versa.</p>
  
  <p>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:</p>
  
  <pre><code>Lists listService = new Lists();
  listService.PreAuthenticate = true;
  listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
  listService.Url = "http://sharepointserver/webname/_vti_bin/Lists.asmx";
  </code></pre>
  
  <p>My CRM server and Sharepoint server are in the same domain.</p>
  
  <p>For both code, if I changed the credentials part into something like this then deploy it on server, it can run.</p>
  
  <pre><code>service.Credentials = new NetworkCredential("username", "password", "domain");
  </code></pre>
  
  <p>Still, I don't want to do this because it reveals user's password in the code. May anyone help me?</p>
  
  <p>The IIS in both server doesn't allow Anonymous Access and it uses Integrated Windows Authentication. </p>
  
  <p>Thank you</p>
  
  <hr>
  
  <p>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 "<strong>Stored Username and Password</strong>" (<em>Control Panel > User Accounts > tab Advanced > Manage Passwords</em>)
  This way, I don't have to type:</p>
  
  <pre><code>service.Credentials = new NetworkCredential("username", "password", "domain");
  </code></pre>
  
  <p>and my DefaultCredentials from my local comp is authorized to access the web services.</p>
  
  <p>I tried to implement this on the Sharepoint server that access CRM web services. and..tadaa..it won't work. hahaha.. </p>
  
  <p>can we inject credentials to DefaultCredentials in server?</p>
  
  <p>the last thing I want to do is to hardcode the useraccount (like the code above)</p>