4
votes

I'm trying to retrieve the logged in windows user in a WCF service. I've tried using ServiceSecurityContext but Current is always null.

ServiceSecurityContext.Current.WindowsIdentity.Name

I've also tried using OperationContext. In this case ServiceSecurityContext returns as null.

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name

Here's my web.config:

 <bindings>
  <basicHttpBinding>
    <binding name="HttpWindowsBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings> 

also

<authentication mode="Windows"/> 

Can anyone see what I'm doing wrong?

UPDATE: I abandoned trying to get ServiceSecurityContext to work. In the end, I found a solution by setting aspNetCompatibilityEnabled="true".

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />

I also added the following attribute to my service class:

[System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Required)]

This allowed me to retrieve the windows user with:

HttpContext.Current.User.Identity.Name
1
I hope you have added this binding configuration "HttpWindowsBinding" to the endpoint in your service.vibhu
I have. Still not working.ijason03

1 Answers

1
votes

I got the same error and managed to solved it, you need not to use basichttpBinding. bellow is my config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
      <bindings>
        <wsHttpBinding>
          <binding name="MSADC">
            <security mode ="Message">
              <transport clientCredentialType="Windows" />
              <message establishSecurityContext="true" />
            </security>
          </binding>
        </wsHttpBinding>
      </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="WCFAuthentication.WCFAuthentication">
                <endpoint bindingConfiguration="MSADC" address="" binding="wsHttpBinding" contract="WCFAuthentication.IWCFAuthentication">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8088" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
  <appSettings>
    <add key="wcf:disableOperationContextAsyncFlow" value="false" />
  </appSettings>
</configuration>