【发布时间】:2015-05-08 00:33:59
【问题描述】:
我了解(大部分)SAML 流程,因为我是在 WIF .NET 4.5 中使用组件空间之前编写的。
我不明白的是如何使用 SP 的证书加密 XML 断言。我找到的只是“它在低级 api 项目中”,但我找不到它。
在方法 SendSAMLResponse 中,我使用我的 pfx 来签署证书。如何使用 SP 的公共证书将断言加密到元素 <saml2:EncryptedAssertion> 中?
我知道你可以使用“高级 API”的方式,你可以在 saml.config 文件中设置一些值来加密它,但是我必须添加更多的属性,我认为我不能使用“高级 API”的方式。
private SAMLResponse CreateSAMLResponse(string username, string uniqueKey)
{
SAMLResponse samlResponse = new SAMLResponse();
samlResponse.Destination = EquatorConstants.ConsumerUrl;
samlResponse.ID = "_" + Guid.NewGuid();
Issuer issuer = new Issuer(EquatorConstants.Issuer);
samlResponse.Issuer = issuer;
samlResponse.Status = new Status(SAMLIdentifiers.PrimaryStatusCodes.Success, null);
SAMLAssertion samlAssertion = new SAMLAssertion();
samlAssertion.Issuer = issuer;
//Subject subject = new Subject(new NameID(User.Identity.Name));
Subject subject = new Subject(new NameID());
SubjectConfirmation subjectConfirmation = new SubjectConfirmation(SAMLIdentifiers.SubjectConfirmationMethods.Bearer);
SubjectConfirmationData subjectConfirmationData = new SubjectConfirmationData();
subjectConfirmationData.Recipient = EquatorConstants.ConsumerUrl;
subjectConfirmation.SubjectConfirmationData = subjectConfirmationData;
subject.SubjectConfirmations.Add(subjectConfirmation);
samlAssertion.Subject = subject;
AuthnStatement authnStatement = new AuthnStatement();
authnStatement.AuthnContext = new AuthnContext();
authnStatement.AuthnContext.AuthnContextClassRef = new AuthnContextClassRef(SAMLIdentifiers.AuthnContextClasses.Password);
authnStatement.AuthnInstant = DateTime.UtcNow;
authnStatement.SessionNotOnOrAfter = DateTime.UtcNow.AddMinutes(double.Parse(SAMLConstants.TokenLifetime.ToString()));
samlAssertion.Statements.Add(authnStatement);
samlAssertion.Conditions.NotBefore = DateTime.UtcNow;
samlAssertion.Conditions.NotOnOrAfter = DateTime.UtcNow.AddMinutes(double.Parse(SAMLConstants.TokenLifetime.ToString()));
samlAssertion.IssueInstant = DateTime.UtcNow;
samlAssertion.Version = "2.0";
AttributeStatement attribStatement = new AttributeStatement();
SAMLAttribute attribute = new SAMLAttribute("UserExternalKey", SAMLIdentifiers.AttributeNameFormats.Unspecified, null, uniqueKey);
attribStatement.Attributes.Add(attribute);
SAMLAttribute attribute2 = new SAMLAttribute("UserType", SAMLIdentifiers.AttributeNameFormats.Unspecified, null, "Workstation");
attribStatement.Attributes.Add(attribute2);
samlAssertion.Statements.Add(attribStatement);
samlResponse.Assertions.Add(samlAssertion);
return samlResponse;
}
private void SendSAMLResponse(SAMLResponse samlResponse, string relayState, HttpResponse response)
{
// Serialize the SAML response for transmission.
XmlElement samlResponseXml = samlResponse.ToXml();
// Sign the SAML response.
X509Certificate2 x509Certificate = (X509Certificate2)LoadCertificate(string.Format("{0}/{1}.pfx", AppDomain.CurrentDomain.BaseDirectory, SAMLConstants.CertificateFileName), SAMLConstants.PfxPassword);
SAMLMessageSignature.Generate(samlResponseXml, x509Certificate.PrivateKey, x509Certificate);
IdentityProvider.SendSAMLResponseByHTTPPost(response, EquatorConstants.ConsumerUrl, samlResponseXml, relayState);
}
【问题讨论】:
标签: c# xml encryption saml-2.0