gpt4 book ai didi

c# - 解决 WCF 错误 : The EncryptedKey clause was not wrapped with the required encryption token 'System.IdentityModel.Tokens.X509SecurityToken'

转载 作者:行者123 更新时间:2023-11-30 12:49:53 29 4
gpt4 key购买 nike

我有一个 WCF 客户端崩溃并出现错误“The EncryptedKey clause was not wrapped with the required encryption token 'System.IdentityModel.Tokens.X509SecurityToken'。”对于每个响应。

我环顾四周,this blog post似乎表明问题出在我的证书设置上,但我不确定我做错了什么......

为了安全起见,我的客户端使用带有 MutualCertificateBindingElement 的自定义绑定(bind),我在代码中配置证书如下:

client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.ChainTrust;
client.ClientCredentials.ServiceCertificate.SetDefaultCertificate
(
StoreLocation.CurrentUser,
StoreName.AddressBook,
X509FindType.FindBySerialNumber,
"[serial number 1]"
);

client.ClientCredentials.ClientCertificate.SetCertificate
(
StoreLocation.CurrentUser,
StoreName.My,
X509FindType.FindBySerialNumber,
"[serial number 2]"
);

序列号与 <X509SerialNumber> 中的值匹配请求和响应消息中的元素。

我注意到的一个差异是 <X509IssuerName>请求和响应中的元素格式不同:

Request:  CN=[CN], O=[O], L=[L], C=[C]
Response: C=[C],L=[L],O=[O],CN=[CN]

这是否可能导致问题?

更新

原来是证书名称格式导致了问题。我设法通过使用自定义编码器将响应中的证书名称替换为 WCF 期望的证书名称来解决它。现在我有了这个丑陋的 hack,但它有效,所以我会接受它!

public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
var msgContents = new byte[buffer.Count];
Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length);
bufferManager.ReturnBuffer(buffer.Array);
var message = Encoding.UTF8.GetString(msgContents);

// Fix certificate issuer name formatting to match what WCF expects.
message = message.Replace
(
"C=[C],L=[L],O=[O],CN=[CN]",
"CN=[CN], O=[O], L=[L], C=[C]"
);

var stream = new MemoryStream(Encoding.UTF8.GetBytes(message));
return ReadMessage(stream, int.MaxValue);
}

最佳答案

您提到的发行者名称顺序很可能是问题所在。由于这些名称未签名,我建议您在客户端中编写一个自定义编码器,以替换响应中的名称以按照请求中的格式进行格式化。

关于c# - 解决 WCF 错误 : The EncryptedKey clause was not wrapped with the required encryption token 'System.IdentityModel.Tokens.X509SecurityToken' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10427246/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com