从WCF服务中访问ServiceSecurityContext.Current.PrimaryIdentity.Name

时间:2013-06-25 14:03:05

标签: wcf

我对WCF很新,但是我已经非常彻底地搜索了这个主题并且没有得到满意的答案,所以这里提出了我的问题:

在我的WCF服务中,我需要访问用户的用户名。从我读过的所有内容中,我应该能够从ServiceSecurityContext.Current.PrimaryIdentity.Name中获取它。但是,它不会像我希望的那样返回Domain \ Username,而是始终返回NT AUTHORITY \ NETWORK SERVICE。如何获取登录到访问我服务的计算机的个人的实际域名和用户名?

感谢。

2 个答案:

答案 0 :(得分:1)

你看过ServiceSecurityContext班了吗?

  

表示远程方的安全上下文。在客户端,   代表服务标识,在服务上代表服务标识   客户身份。

e.g。

ServiceSecurityContext.Current.WindowsIdentity.Name

...确保您已将服务设置为通过Windows安全进行身份验证。

答案 1 :(得分:0)

要使用Windows凭据,请将clientCredentialType设置为“Windows”。使用wsHttpBinding,或netTcpBinding,如果它在LAN

    <bindings>
  <netTcpBinding>
    <binding name="WindowsCredentials">
      <security mode="Transport">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </netTcpBinding>
</bindings>