WCF服务中的身份验证

时间:2009-08-31 06:18:19

标签: wcf wcf-security wcf-client

我在另一台机器上部署了WCF服务,我想根据WCF服务对客户端进行身份验证。

我做了以下事情:

1)在IIS中,我取消选中匿名访问并选中“集成Windows身份验证”复选框。

2)我的网络配置

 <authentication mode="Windows" />
 <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBind">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

3)在客户端,我传递的用户凭证如下:

MyServiceClient _client;

_client = new MyServiceClient();

_client.ClientCredentials.Windows.ClientCredential.UserName = "username";
_client.ClientCredentials.Windows.ClientCredential.Password = "password";
_client.ClientCredentials.Windows.ClientCredential.Domain = "mydomain";

我的问题是如何在服务器端(部署服务的位置)捕获用户名和密码?

如何根据传递的凭据对用户进行身份验证?

目前我正在使用basichttp绑定..这种绑定是否足以支持安全模型?

2 个答案:

答案 0 :(得分:6)

在服务器端,您可以使用传入的Windows凭据对Active Directory进行身份验证,或者您需要使用备用存储来处理用户身份验证。

您可以使用以下方法在服务器端代码中访问来电者的身份:

IIdentity caller = ServiceSecurityContext.Current.PrimaryIdentity;

您还可以通过检查

来检查Windows用户是否使用其Windows凭据进行调用(如示例中所示)
ServiceSecurityContext.Current.WindowsIdentity

如果它为NULL,则没有传递任何Windows凭据 - 否则您可以使用此Windows身份来检查谁正在呼叫(名称等) - 但您将无法读取用户的密码!你可以查看他的名字,他所属的团体等等。

要使用Windows / Active Directory验证,请将clientCredentialType设置为“Windows”。您可能必须切换到wsHttpBinding,甚至更好:netTcpBinding(如果您位于防火墙后面的Windows LAN上)。

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

这样,只有在Windows域中注册的用户才能调用该服务。任何其他用户将被拒绝,而无需任何额外的工作。

一旦您有Windows用户呼叫,您可以查看ServiceSecurityContext.Current.WindowsIdentity以获取有关谁正在呼叫的信息。

检查MSDN docs以获取有关服务安全上下文或Windows identity上可用内容的详细信息。

马克

答案 1 :(得分:0)

您似乎需要自定义用户名和密码验证程序。有一篇MSDN文章涵盖了所有步骤:How to: Use a Custom User Name and Password Validator

BasicHttpBinding支持多种安全模式。如果您使用重载的构造函数,则可以为BasicHttpSecurityMode传入您选择的值。

相关问题