gpt4 book ai didi

wcf - 使用证书加密数据时如何使 WIF 使用 RSA15?

转载 作者:行者123 更新时间:2023-12-01 19:36:57 25 4
gpt4 key购买 nike

我一直在开发一个 WCF 服务,该服务将返回一个 Base64 编码的字符串,实际上,它是一个完整的 SAML 响应 XML 文档。由于此信息将移交给供应商,因此我必须满足他们对 SAML 文档的外观和编码方式的要求。我无法获得满足他们要求的输出。

我知道 WCF 和 WIF 一起应该对我有帮助。我最初使用 WIF 构建服务来创建 SAML 断言( token )和其他 C# 代码来生成最终的 SAML 文档。除文档的 节点外,所有这些都有效并满足供应商的要求。本部分使用 AES256 和 RSAOAEP,但供应商希望使用 AES128 和 RSA15。因此,我正在寻找解决方案。任何帮助将不胜感激。

这是一个演练。

该服务接受一个 GUID,用于调用数据库并返回字段。然后像这样使用它们:

DataTable userData = GetDataForUser(userId);
List<Claim> claims = new List<Claim>()
{
new Claim("ClientId", "NameOfClient")
};
foreach (DataRow row in userData.Rows)
{
string memberId = row["MemberId"].ToString().Trim();
string firstName = row["FirstName"].ToString().Trim();
string lastName = row["LastName"].ToString().Trim();
DateTime dob = Convert.ToDateTime(row["DateOfBirth"], CultureInfo.InvariantCulture);

claims.Add(new Claim("MemberId", memberId));
claims.Add(new Claim("FirstName", firstName));
claims.Add(new Claim("LastName", lastName));
claims.Add(new Claim("DOB", dob.ToString("MM/dd/yyyy")));
}

return claims;

然后我创建一个 SecurityTokenDescriptor,如下所示:

SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor();

声明被添加到描述符中,如下所示:

descriptor.Subject = new ClaimsIdentity(claims);

指示描述符对 token 进行加密,如下所示:

descriptor.EncryptingCredentials = GetEncryptingCredentials();

GetEncryptingCredentials() 例程如下所示:

private EncryptedKeyEncryptingCredentials GetEncryptingCredentials()
{
// Get the Encrypting Certificate
X509Certificate2 encryptCert = CertificateHelper.FindSingleCertificate(StoreName.TrustedPeople, StoreLocation.LocalMachine, X509FindType.FindBySubjectDistinguishedName, "<<certificate stuff here >>", true);

EncryptedKeyEncryptingCredentials encryptingCreds = new EncryptedKeyEncryptingCredentials(encryptCert);

return encryptingCreds;
}

所有这些都会生成一个 token ,当写入文件时,该 token 会给出以下内容:

  <EncryptedAssertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
<xenc:EncryptedData Id="_16584ace-9f3e-4352-9fc9-f6db8b2e925c" Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<e:EncryptedKey xmlns:e="http://www.w3.org/2001/04/xmlenc#">
<e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
</e:EncryptionMethod>
<KeyInfo>
<o:SecurityTokenReference xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<X509Data>
<X509IssuerSerial>
<X509IssuerName><!-- value --></X509IssuerName>
<X509SerialNumber><!-- value --></X509SerialNumber>
</X509IssuerSerial>
</X509Data>
</o:SecurityTokenReference>
</KeyInfo>
<e:CipherData>
<e:CipherValue><!-- value -->CipherValue>
</e:CipherData>
</e:EncryptedKey>
</KeyInfo>
<xenc:CipherData><xenc:CipherValue><!-- value --></xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</EncryptedAssertion>

太棒了,对吧?没有。供应商需要 部分具有以下子节点:

<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>

他们需要 部分来显示这一点:

<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>

我已经尝试了 GetEncryptingCredentials() 例程中我能想到的所有组合。一切都没有产生预期的结果。我收到的最有希望的错误消息如下所示:

ID4178: The EncryptingCredentials provided in the SecurityTokenDescriptor is for an asymmetric key. You must use an EncryptedKeyEncryptingCredentials to encrypt the token.

有人有建议吗?不要害怕告诉我一切从头开始。没关系。我只需要让它发挥作用。

提前致谢。

最佳答案

我找到了一个有效的解决方案。至少,它生成了我需要的 XML,并且供应商表示他们能够使用我发送给他们的内容。

我稍微重写了 GetEncryptingCredentials() 例程。现在看起来像这样:

private EncryptingCredentials GetEncryptingCredentials()
{
string keyWrapAlgorithm = SecurityAlgorithms.RsaV15KeyWrap; //"http://www.w3.org/2001/04/xmlenc#aes256-cbc";
string encryptionAlgorithm = SecurityAlgorithms.Aes128Encryption; //"http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p";
int keySize = 128;

X509Certificate2 encryptCert = CertificateHelper.FindSingleCertificate(StoreName.TrustedPeople, StoreLocation.LocalMachine, X509FindType.FindBySubjectDistinguishedName, _settings.EncryptingCredentials, true);

EncryptingCredentials encryptingCredentials = new EncryptedKeyEncryptingCredentials(encryptCert, keyWrapAlgorithm, keySize, encryptionAlgorithm);

return encryptingCredentials;
}

只是想让每个人都知道并结束这个循环。

关于wcf - 使用证书加密数据时如何使 WIF 使用 RSA15?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4196278/

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