如何解决如何将具有联合绑定的 wcf 客户端从 .net 框架迁移到 .net 核心?
我正在将 WCF 客户端从 .Net Framework 迁移到 .Net 核心。我正在创建从 ClientBase 派生的客户端并使用联合绑定。
这是在 .Net Framework 中工作的绑定创建代码:
private Binding CreateBinding()
{
var issuerBinding = new WSHttpBinding(SecurityMode.Transport);
issuerBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.IssuedKeyType = SecurityKeyType.SymmetricKey;
binding.Security.Message.NegotiateServiceCredential = false;
binding.Security.Message.EstablishSecurityContext = false;
binding.Security.Message.IssuerAddress = new EndpointAddress(this.stsAddress);
binding.Security.Message.IssuerBinding = issuerBinding;
return binding
}
这里是.Net core中对应的代码:
private Binding CreateBinding()
{
var issuerBinding = new WSHttpBinding(SecurityMode.Transport);
issuerBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var endpointAddress = new EndpointAddress(this.stsAddress);
var tokenParameters = WSTrustTokenParameters.CreateWS2007FederationTokenParameters(issuerBinding,endpointAddress);
tokenParameters.KeyType = SecurityKeyType.SymmetricKey;
var binding = new WSFederationHttpBinding(tokenParameters);
binding.Security.Message.NegotiateServiceCredential = false;
binding.Security.Message.EstablishSecurityContext = false;
return binding;
}
不幸的是,.net 核心版本不起作用 - 调用该服务会抛出“HTTP 请求被禁止使用客户端身份验证方案“匿名”。
结果是对sts的请求失败了。 我创建了一个代理来拦截 http 请求,发现对 sts 服务的调用存在以下差异:
- .net 框架请求包含 .net 核心版本中缺少的以下属性:
<trust:KeyWrapAlgorithm>
http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p
</trust:KeyWrapAlgorithm>
<trust:EncryptWith>
http://www.w3.org/2001/04/xmlenc#aes256-cbc
</trust:EncryptWith>
<trust:SignWith>
http://www.w3.org/2000/09/xmldsig#hmac-sha1
</trust:SignWith>
<trust:CanonicalizationAlgorithm>
http://www.w3.org/2001/10/xml-exc-c14n#
</trust:CanonicalizationAlgorithm>
<trust:EncryptionAlgorithm>
http://www.w3.org/2001/04/xmlenc#aes256-cbc
</trust:EncryptionAlgorithm>
-
trust:Entropy
元素是不同的。 .net 框架版本包含类型属性Type="http://docs.oasis-open.org/ws-sx/ws-trust/200512/Nonce"
,该属性在 .net 核心版本中缺失。根据 WS-Trust 文档,默认值为 SymmetricKey。 - .net 核心请求包含 .net 框架版本中缺少的
trust:TokenType
元素
trust:BinarySecret
的 <trust:TokenType>
http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0
</trust:TokenType>
如何设置适当的绑定以使其在 .net core 中工作?
解决方法
Core 不支持 Wshttpbinding,wcf 只支持 BasicHttpBinding、CustomBinding、NetHttpBinding、NetTcpBinding:
建议你继续使用.net框架或者修改服务端绑定。
有关核心中 WCF 功能的更多信息,您可以参考此link。
,事实证明,.net core WCF 客户端没有随请求发送证书。 .net core 检查证书是否有私钥,如果没有则跳过。 https://github.com/dotnet/corefx/blob/fe7adb2bf92bba926268c2e9f562b11c84932a07/src/Common/src/System/Net/Security/CertificateHelper.cs#L39-L42
if (!cert.HasPrivateKey)
{
continue;
}
我从 Azure Key Vault 获取证书,默认情况下它会在没有私钥的情况下返回证书。将其更改为包含私钥解决了问题。
我的 .net framework 应用程序使用相同的方法获取证书(因此它也没有私钥),但它能够从安装了证书的本地机器存储中获取私钥。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。