【发布时间】:2010-11-17 21:26:18
【问题描述】:
我一直在开发 WCF 服务,该服务将返回一个 Base64 编码的字符串,实际上是一个完整的 SAML 响应 XML 文档。因为这些信息将被传递给供应商,所以我必须满足他们对 SAML 文档的外观和编码方式的要求。我无法获得满足他们要求的输出。
我知道 WCF 和 WIF 一起应该可以帮助我。我最初使用 WIF 构建服务来创建 SAML 断言(令牌)和其他 C# 代码以生成最终的 SAML 文档。除文档的
这里是一个演练。
该服务接受一个用于调用数据库和返回字段的 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);
指示描述符像这样加密令牌:
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;
}
所有这些都会生成一个令牌,当写入文件时会给出这个:
<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:加密凭据 中提供的 SecurityTokenDescriptor 用于 非对称密钥。您必须使用 EncryptedKeyEncryptingCredentials 到 加密令牌。
有人有建议吗?不要害怕告诉我重新开始。没关系。我只需要让它工作。
提前致谢。
【问题讨论】: