WCF客户端证书验证+ Windows身份验证

时间:2017-09-04 07:01:51

标签: c# wcf authentication ssl iis

我已成功创建了一个WCF服务,该服务根据IIS中配置的链验证传入的客户端证书。但是,由于这只是一种支持身份验证的安全机制,我还需要Windows用户调用我的WCF服务来处理授权。

通常在解压缩Windows用户时,您可以这样做

ServiceSecurityContext.Current.WindowsIdentity.Name

当我的服务配置了安全模式TransportWithMessageCredentials时,PrimaryIdentity中的ServiceSecurityContext将返回证书SubjectNameWindowsIdentity将是空的。

要查看客户端配置,我已经指定了WsHttpBinding这样的

private static Binding GetHttpsBinding()
{
    var binding = new WSHttpBinding();
    binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;

    return binding;
}

客户端证书像代理客户端一样添加,如下所示:

private static void ApplyClientCertificate(HelloServiceClient client)
{
    client.ClientCredentials.ClientCertificate.SetCertificate(

        storeLocation: StoreLocation.CurrentUser,
        storeName: StoreName.My,
        findType: X509FindType.FindBySubjectName,
        findValue: "ClientCertificatesTest"

   );
}

切换两个ClientCredentialType值,使绑定看起来像这样

private static Binding GetHttpsBinding()
{
    var binding = new WSHttpBinding();
    binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

    return binding;
}

将如上所述提取Windows凭据,但是当提交无效证书或根本没有证书时也会被接受!因此,不满足认证要求。我还可以补充说,当以这种方式配置时,我在服务器端的X509CertificateValidator实现不会触发,因此我怀疑客户端证书没有被添加。

当然必须有一些方法来添加客户端证书进行身份验证并添加Windows凭据以处理WCF中的授权?除了将其添加到客户端凭据之外,还有其他方法可以添加证书吗?

提前致谢!

2 个答案:

答案 0 :(得分:0)

因此,这个问题的答案是创建自己的CustomBinding以获得Windows Credentails和证书验证。

答案 1 :(得分:0)

使用 Web 服务引用,您可以同时提供客户端证书和 Windows 身份验证凭据,所以奇怪的是,WCF 不能开箱即用?

您是否实现了自定义绑定或提供了任何示例链接以使其正常工作?

更新:这是创建自定义绑定以获取 Windows 身份验证和客户端证书的解决方案。 http://david-homer.blogspot.com/2021/05/using-net-wcf-basichttpbinding-to.html

谢谢,

戴夫