WCF中的ServiceSecurityContext.Current为null

时间:2013-07-12 16:03:45

标签: wcf silverlight

我正在尝试在WCF服务中检索登录的Windows用户。 我尝试过使用ServiceSecurityContext,但Current始终为null。

ServiceSecurityContext.Current.WindowsIdentity.Name

我也尝试过使用OperationContext。在这种情况下,ServiceSecurityContext返回null。

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name

这是我的web.config:

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

<authentication mode="Windows"/> 

谁能看到我做错了什么?

更新: 我放弃了尝试让ServiceSecurityContext工作。最后,我通过设置aspNetCompatibilityEnabled =“true”找到了解决方案。

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

我还在服务类中添加了以下属性:

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

这允许我使用以下命令检索Windows用户:

HttpContext.Current.User.Identity.Name

2 个答案:

答案 0 :(得分:1)

我遇到了同样的错误并设法解决了这个问题,您无需使用basichttpBinding。波纹管是我的配置:

<?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>

答案 1 :(得分:-2)

这是一篇旧帖子,但是去here ...

您需要设置TransportCredentialOnly