gpt4 book ai didi

c# - 我在将此 SAMLResponse 读入 SecurityToken 对象时遇到问题

转载 作者:行者123 更新时间:2023-11-30 23:31:41 28 4
gpt4 key购买 nike

我正在运行 Shibboleth SSO 登录页面,当我成功验证自己时,它会使用 SAMLResponse 字符串执行 POST,该字符串包含对我的 .NET MVC 应用程序的 base-64 编码 SAML 响应。我正在尝试将其解析为 SecurityToken对象,如果可能的话:

var tokenString = Encoding.UTF8.GetString(Convert.FromBase64String(base64EncodedSamlToken));
var token = System.IdentityModel.Services.FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers.ReadToken(new XmlTextReader(new StringReader(tokenString)));

token的值一直是null,所以解析不正确,这是为什么呢?

在 token 的 XML 中,我在其他 XML 标记和数据中看到以下响应标记:

<saml2p:Response Destination="https://blah.local/" ID="_be8cc118c528ba0407446b8cc2dca019" InResponseTo="ID-302e3473-f063-43b0-b8e4-b8f47fbbe350" IssueInstant="2015-12-29T16:19:35.603Z" Version="2.0" xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">https://shibboleth.blah.local/idp/shibboleth</saml2:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">

我的印象是内置的 SecurityTokenHandlers 可以读取这种类型的响应。不是这样吗?不是很清楚,如there was an extension for WIF awhile ago to allow SP-initiated SSO ,但据我所知,不得用于生产用途;虽然,下载它的链接现在已经失效(很像微软的所有开发门户链接,当你真的需要一些东西时)。我的主要理解是,这与 SAML 2.0 安全 token 的不同之处在于,这是 SAML-P 2.0,其中 P 代表“协议(protocol)”,WIF 在这么多年后仍然不完全支持 SAML 2.0 协议(protocol),但确实支持SAML 2.0 token 。什么情况?也许有人愿意详细说明? 更新:确实,正如其他人评论的那样,WIF 仍然不支持 SAML-P,即使经过了这么多年。

我的默认 SecurityTokenHandlers 集合包括以下不同的处理程序,它们似乎都不想处理此响应:

SamlSecurityTokenHandler
Saml2SecurityTokenHandler
WindowsUserNameSecurityTokenHandler
X509SecurityTokenHandler
KerberosSecurityTokenHandler
RsaSecurityTokenHandler
SessionSecurityTokenHandler
EncryptedSecurityTokenHandler

如何从此 SAMLResponse 中获取 SecurityToken?

最佳答案

我找到了解决办法。一旦我将 SAMLResponse 作为字符串获取,我就可以反序列化它,但是反序列化的方法非常细致,因为编码是在构建 token 时导致 Saml2SecurityToken 构造函数失败的必经之路(这肯定是个坏蛋)。

想法很简单,从 XML 中获取 saml2:Assertion 标记作为元素,并使用它来构建 SecurityToken;它经过测试并适用于我的情况,因此希望它可以帮助其他人:

var samlToken = (SecurityToken)null;
var form = await Request.ReadFormAsync(); // I am using IOwinRequest here, but if you are using something else you can probably fetch this parameter from somewhere within HttpContext.Current.Request.Form["SAMLResponse"]; or elsewhere.
if (form.Count() > 0)
{
var samlResponses = form.GetValues("SAMLResponse");
if (samlResponses != null && samlResponses.Count > 0)
{
foreach (var samlResponse in samlResponses)
{
try
{
var decodedSamlResponse = Convert.FromBase64String(samlResponse);
var reader = XmlReader.Create(new MemoryStream(decodedSamlResponse));
var serializer = new XmlSerializer(typeof(XmlElement));
var samlResponseElement = (XmlElement)serializer.Deserialize(reader);
var manager = new XmlNamespaceManager(samlResponseElement.OwnerDocument.NameTable);
manager.AddNamespace("saml2", "urn:oasis:names:tc:SAML:2.0:assertion");
var assertion = (XmlElement)samlResponseElement.SelectSingleNode("//saml2:Assertion", manager);
samlToken = (Saml2SecurityToken)Options.SecurityTokenHandlers.ReadToken(XmlReader.Create(new StringReader(assertion.OuterXml)));
break;
}
catch { }
}
}
}

关于c# - 我在将此 SAMLResponse 读入 SecurityToken 对象时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34516937/

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