【发布时间】:2018-02-12 09:13:22
【问题描述】:
我已经成功创建了一个 WCF 服务,该服务根据 IIS 中配置的链验证传入的客户端证书。但是,由于这只是一种支持身份验证的安全机制,因此我还需要 Windows 用户调用我的 WCF 服务来处理授权。
通常在提取 Windows 用户时,您会这样做
ServiceSecurityContext.Current.WindowsIdentity.Name
当我的服务配置安全模式TransportWithMessageCredentials 时,ServiceSecurityContext 中的PrimaryIdentity 将返回证书的SubjectName,WindowsIdentity 将为空。
为了查看客户端配置,我指定了 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 中的授权?除了将证书添加到客户端凭据之外,还有其他方法可以添加证书吗?
提前致谢!
【问题讨论】:
标签: c# wcf authentication ssl iis