title: Get username in WCF service using basic auth with IIS
body: <p>I have a WCF service running in IIS Express on my local machine. I'm using HTTP Basic Authentication (without SSL at the moment). I have a test client that I'm using to call my server.</p>
  
  <p>I need the name of the user that initiated the request (the username portion of the basic auth authentication). I realize that IIS is handling the authentication for me and is checking the username/password against Windows user accounts. That is fine for my purposes. My issue is that once my service is called I can't find the username anywhere. I assumed it would be in the <code>Thread.CurrentPrincipal.Identity.Name</code>, but that value is an empty string. Is there a way to access this value?</p>
  
  <p>Here is binding in case it is relevant:</p>
  
  <pre><code>&lt;basicHttpBinding&gt;
  &lt;binding name="basicauth" closeTimeout="00:01:00" openTimeout="00:01:00" 
  receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" 
  bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
  maxBufferSize="99999999" maxBufferPoolSize="524288" maxReceivedMessageSize="99999999" 
  messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
  useDefaultWebProxy="true"&gt;
        &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
  maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
    &lt;security mode="TransportCredentialOnly"&gt;
      &lt;transport clientCredentialType="Basic" realm="" /&gt;
    &lt;/security&gt;          
  &lt;/binding&gt;
  &lt;/basicHttpBinding&gt;
  </code></pre>
  
  <p>UPDATE: Thanks everyone. Figured out my issue. I had improperly associated my endpoint and my binding so the endpoint was defaulting to some dynamic binding. Even though I configured my binding for basic auth the endpoint was not setup to use it. </p>
  
  <p>Once I fixed this issue the username was present in the </p>
  
  <pre><code>ServiceSecurityContext.Current.WindowsIdentity.Name
  </code></pre>
  
  <p>property like expected.</p>