.net - 阅读使用 WSSecurityTokenSerializer 在 .Net 中验证 SAML 断言的问题

标签 .net single-sign-on x509certificate pkcs#7 saml-2.0

我有一个 SAML 断言,我希望使用 WSSecurityTokenSerializer 在 .Net 中进行身份验证。

尽管有 a few issues,但我有 key 链和 SAML XML .

首先,我从 HTTPS POST 获得 SAML 断言:

// spec says "SAMLResponse=" 
string rawSamlData = Request["SAMLResponse"];

// read the base64 encoded bytes
byte[] samlData = Convert.FromBase64String(rawSamlData);

// read back into a UTF string
string samlAssertion = Encoding.UTF8.GetString(samlData);

// get the SAML data in an XML reader
var assertionPostStream = new StringReader(samlAssertion);
var reader = XmlReader.Create(assertionPostStream);

然后我得到我的 IdP 提供的 key :

// get the key data
byte[] certificateData = System.IO.File.ReadAllBytes("myKeys.p7b");

// decode the keys
var cms = new SignedCms(SubjectIdentifierType.IssuerAndSerialNumber);
cms.Decode(certificateData);

// we have a keychain of X509Certificate2s, we need a collection of tokens
var certificatesAsTokens =
    from X509Certificate2 cert in cms.Certificates
    select new X509SecurityToken(cert) as SecurityToken;

// get a token resolver
var tokens = new ReadOnlyCollection<SecurityToken>(
    certificatesAsTokens.ToList());
var resolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(
    tokens, true);

最后我在这里抛出一个错误:

// use the WS Security stuff to parse the reader
var securityToken = WSSecurityTokenSerializer.
    DefaultInstance.ReadToken(reader, resolver) as SamlSecurityToken;

调用 ReadToken 时出现以下错误:

Cannot read the token from the 'Response' element with the 'urn:oasis:names:tc:SAML:2.0:protocol' namespace for BinarySecretSecurityToken, with a '' ValueType. If this element is expected to be valid, ensure that security is configured to consume tokens with the name, namespace and value type specified.

我的 SAML XML 开始于:

<Response xmlns="urn:oasis:names:tc:SAML:2.0:protocol" ...

很明显,我在 urn:oasis:names:tc:SAML:2.0:protocol 命名空间中有一个 Response 元素。

知道这里出了什么问题/遗漏了什么吗?

最佳答案

您似乎收到了 SAML2 响应。尽管 .NET 4.5 中支持 SAML2,但遗憾的是仅支持断言 - 不支持协议(protocol)本身(包括响应消息)。

要在 .NET 中处理 SAML2 响应,您必须:

  1. 验证整个响应消息的签名。
  2. 提取消息的断言部分。
  3. 使用 Saml2SecurityTokenHandler.ReadToken() 读取 token 。
  4. 使用 Saml2SecurityTokenHandler.DetectReplayedToken() 验证 token 。
  5. 使用 Saml2SecurityTokenHandler.ValidateConditions() 验证 token
  6. 使用 Saml2SecurityTokenHandler.CreateClaims() 创建声明标识。

不幸的是,这些方法中的大多数都受到保护,但您可以子类化 Saml2SecurityTokenHandler 并访问它们。

可以在 Saml2Response 中找到完整的工作示例Sustainsys.Saml2 中的类项目。

关于.net - 阅读使用 WSSecurityTokenSerializer 在 .Net 中验证 SAML 断言的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6108564/

相关文章:

java - 如何解密 LTPA token 版本 2

ajax - 使用 SSO : Ajax request

java - 我可以在不重新启动 JVM 的情况下将新证书添加到 keystore 吗?

.net - HtmlUnit 不等待 AJAX 执行

.net - 使用对象作为字典键

.net - 组件架构和 DI 容器的使用?

.net - 使用备份 appender 进行日志记录

azure - 为什么用户添加到用户列表后需要在 Azure Single Sign On 中请求管理员批准?

ssl-certificate - Windows : How to import when certificate and private key are in separate files?

identityserver4 - 使用带有 IdentityServer 4 AddSigningCredential 的 Linux 容器在 Azure 应用服务上加载自签名证书时出错