title: Getting WindowsIdentity in my WCF Web Service
body: <p>I took over code from a developer that is no longer with us. It's a WCF web service that was originally using a passed in username, but we need it to use the WindowsIdentity instead.</p>
  
  <pre><code>string identity = ServiceSecurityContext.Current.WindowsIdentity.Name;
  </code></pre>
  
  <p>That code ends up returning an empty string. I'm using a secure (wsHttpSecure) binding so ServiceSecurityContext.Current isn't null or anything. I've been searching for a solution for a day and haven't found anything yet.</p>
  
  <p>Because I'm new to WCF I'm not sure what other information will be relevant. Here are the enabled authentication settings for the web service in IIS:</p>
  
  <pre><code>Anonymous Authentication - Enabled
  Windows Authentication - Enabled
  </code></pre>
  
  <p>And here's the web.config for the web service:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration&gt;
      &lt;connectionStrings&gt;
          &lt;clear /&gt;
          &lt;add name="LocalSqlServer" connectionString="Data Source=.\instanceNameHere;Initial Catalog=default;Integrated Security=SSPI;"/&gt;
      &lt;/connectionStrings&gt;
      &lt;appSettings configSource="appSettings.config" /&gt;
      &lt;system.diagnostics&gt;
          &lt;sources&gt;
              &lt;source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"&gt;
                  &lt;listeners&gt;
                      &lt;add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\ServiceLogs\WebServiceLog.svclog" /&gt;
                  &lt;/listeners&gt;
              &lt;/source&gt;
          &lt;/sources&gt;
      &lt;/system.diagnostics&gt;
      &lt;system.web&gt;
          &lt;trace enabled="true" /&gt;
          &lt;membership defaultProvider="XIMembershipProvider" userIsOnlineTimeWindow="30"&gt;
              &lt;providers&gt;
                  &lt;clear/&gt;
                  &lt;add name="XIMembershipProvider" type="LolSoftware.MiddleTier.BusinessLogic.XIMembershipProvider"
        applicationName="LolWebService"/&gt;
              &lt;/providers&gt;
          &lt;/membership&gt;
          &lt;compilation debug="true" targetFramework="4.0" /&gt;
      &lt;/system.web&gt;
      &lt;system.serviceModel&gt;
          &lt;client /&gt;
          &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
          &lt;behaviors configSource="behaviors.config" /&gt;
          &lt;bindings configSource="bindings.config" /&gt;
          &lt;services configSource="services.config" /&gt;
      &lt;/system.serviceModel&gt;
      &lt;system.webServer&gt;
          &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
          &lt;handlers&gt;
              &lt;remove name="svc-ISAPI-4.0_64bit"/&gt;
              &lt;remove name="svc-ISAPI-4.0"/&gt;
              &lt;remove name="svc-Integrated-4.0"/&gt;
              &lt;add name="svc-ISAPI-4.0_64bit" path="*.svc" verb="*" modules="IsapiModule" scriptProcessor="%systemroot%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv4.0,bitness64" /&gt;
              &lt;add name="svc-ISAPI-4.0" path="*.svc" verb="*" modules="IsapiModule" scriptProcessor="%systemroot%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv4.0,bitness32" /&gt;
              &lt;add name="svc-Integrated-4.0" path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="Unspecified" preCondition="integratedMode" /&gt;
          &lt;/handlers&gt;
      &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>As well as bindings.config:</p>
  
  <pre><code>&lt;bindings&gt;
      &lt;wsHttpBinding&gt;
      &lt;binding name="wsHttpSecure"&gt;
          &lt;security mode="TransportWithMessageCredential"&gt;
              &lt;transport clientCredentialType="None" /&gt;
              &lt;message clientCredentialType="UserName" /&gt;
          &lt;/security&gt;
      &lt;/binding&gt;
      &lt;binding name="wsHttp"&gt;
          &lt;security mode="None" /&gt;
      &lt;/binding&gt;
     &lt;/wsHttpBinding&gt;
  &lt;/bindings&gt;
  </code></pre>
  
  <p>Behaviors.config:</p>
  
  <pre><code>&lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
          &lt;behavior name="serviceBehavior"&gt;
              &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
              &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
              &lt;serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200" /&gt;
              &lt;serviceCredentials&gt;
                  &lt;userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="XIMembershipProvider"/&gt;
              &lt;/serviceCredentials&gt;
          &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
      &lt;!-- --&gt;
      &lt;endpointBehaviors&gt;
          &lt;behavior name="restBehavior"&gt;
              &lt;webHttp/&gt;
          &lt;/behavior&gt;
      &lt;/endpointBehaviors&gt;
      &lt;!-- --&gt;
  &lt;/behaviors&gt;
  </code></pre>
  
  <p>Service.config:</p>
  
  <pre><code>&lt;services&gt;
      &lt;service name="LolSoftware.MiddleTier.WebService.LolWebService" behaviorConfiguration="serviceBehavior"&gt;
          &lt;endpoint name="LolWebService_WSHttpEndpointSecure" contract="LolSoftware.MiddleTier.Interfaces.ILolWebService" binding="wsHttpBinding" bindingConfiguration="wsHttpSecure"/&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
      &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>