title: How to pass Current User Credentials to .asmx Web Service
body: <p><strong>Situation:</strong></p>
  
  <p>I created a web service that runs on my intranet web server.
  The service has an operation that calls Microsoft SparePoint's lists.asmx web service.
  The service also has a test operation.
  When I invoke these services locally (visual studio) everything works great.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>When I deploy this web service to IIS on the web server the operation that calls lists.asmx fails with a 401 error. This occurs both when I run the service from IE when logged into the web server or if I'm running the service remotely (I have custom errors set to off, so I can see the 401 error)</p>
  
  <p><strong>Desired Result:</strong></p>
  
  <p>My web service should take the current user's credentials and pass them to the lists.asmx web service.</p>
  
  <p><strong>Current Code:</strong>
  setting the Credentials in web service:</p>
  
  <pre><code>sharepointService.Lists listService = new sharePointService.Lists();
  listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
  </code></pre>
  
  <p>setting the web reference in web.config:</p>
  
  <pre><code>  &lt;applicationSettings&gt;
      &lt;MyServices.Properties.Settings&gt;
        &lt;setting name="sharepointService_Lists" serializeAs="String"&gt;
          &lt;value&gt;http://sharepoint.myDomain.com/_vti_bin/lists.asmx&lt;/value&gt;
        &lt;/setting&gt;
      &lt;/MyServices.Properties.Settings&gt;
    &lt;/applicationSettings&gt;
  </code></pre>
  
  <p><strong>Other Information:</strong></p>
  
  <p>The Test operation works when deployed on the IIS web server and returns two string for the following: </p>
  
  <p><code>HttpContext.Current.Request.LogonUserIdentity.Name</code> = <code>NT AUTHORITY\IUSR</code></p>
  
  <p><code>System.Security.Principal.WindowsIdentity.GetCurrent().Name</code> = <code>IIS APPPOOL\ASP.NET v4.0</code></p>
  
  <p>So it seems like the web serivce is using the user name of the IIS app pool, if this provides any helpful information.</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>Ideas? Thanks!</p>