title: Is it possible to call a c# WebService with current windows user credentials?
body: <p>I have a small C# ASP.Net WebService. I like to call this WebService from a windows Client and do some things on a SharePoint Document Library. (e.g. uploading a file)</p>
  
  <p>But if I upload a file it will be executed with the credentials from the webservice instead of the credentials from the User who invoked the WebService. Result = All files will be displayed as "created by" = System Account...</p>
  
  <p>Is it possible to call this WebService (from browser or Script $.Ajax) with the current Windows User Credentials and to work with these credentials in the WebService?</p>
  
  <p>I know that I can display the user who invoked the WebService for example in this way:</p>
  
  <pre><code>return Username = User.Identity.Name;
  </code></pre>
  
  <p>But you can not use it to work with these type of credentials?</p>
  
  <p>I already tried several things but I was not able to get the current User.</p>
  
  <p><strong>Code Example1:</strong> (SharePoint Client Object Model)</p>
  
  <pre><code>ClientContext context = new ClientContext("http://domain.local");
  context.Credentials = System.Net.CredentialCache.DefaultCredentials;
  </code></pre>
  
  <p><strong>Code Example2:</strong> (SharePoint Server Object Model)</p>
  
  <pre><code>SPUserToken userToken = null;
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
      using (SPSite site = new SPSite("SiteUrl"))
      {
          using (SPWeb web = site.OpenWeb())
          {
              userToken = web.AllUsers["DOMAIN\\USERNAME"].UserToken;
      }
  });
  
      using (SPSite site = new SPSite("SiteUrl", userToken))
      {
          using (SPWeb web = site.OpenWeb())
          {
              // Perform actions       
          }
      }
  </code></pre>
  
  <p><strong>Code Example3:</strong> (I don´t like that one)
  I found one possible way but I have to type the password plain in code what I don´t like:</p>
  
  <pre><code>NetworkCredential credentials = new NetworkCredential("username", "pwd", "domain");
  
  ClientContext context = new ClientContext("http://site-url");
  context.Credentials = credentials;
  ...
  </code></pre>
  
  <p>I think the most important things in the web.config file are also here?:</p>
  
  <pre><code>&lt;authentication mode="Windows"/&gt;
  &lt;add name="Access-Control-Allow-Origin" value="http://domain.local"/&gt;
  &lt;add name="Access-Control-Allow-Credentials" value="true"/&gt;
  &lt;add name="Access-Control-Allow-Headers" value="Content-Type"/&gt;
  </code></pre>
  
  <p>Any ideas how I can execute the Code with the user context from the current user?</p>
  
  <p><strong>Script Call:</strong></p>
  
  <pre><code>$.ajaxSetup({
      type: "GET",
      url: "webServiceUrl",
      data: { string: "value" },
      dataType: 'json',
      xhrFields: {
         withCredentials: true
      },
      crossDomain: true,
  
      success: function(data){
     },
     error: function(XMLHttpRequest, textStatus, errorThrown){
     }
  });
  </code></pre>