3

我在另一台机器上部署了 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绑定..这个绑定是否足以支持安全模型?

4

2 回答 2

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 文档以获取有关服务安全上下文或Windows 身份可用内容的详细信息。

马克

于 2009-08-31T06:38:49.083 回答
0

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

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

于 2009-08-31T06:28:19.400 回答