1

我有一个来自我的应用程序 IdP 的有效 SAML 2 令牌:

当我尝试使用如下 WIF 代码读取它时,出现以下错误:

无法从具有 BinarySecretSecurityToken 的“urn:oasis:names:tc:SAML:2.0:protocol”命名空间的“Response”元素中读取令牌,并带有“”ValueType。如果预计此元素有效,请确保将安全性配置为使用指定名称、命名空间和值类型的令牌。

这是我正在使用的代码,其中有一条注释显示它失败的地方

        string certPath = @"G:\Projects\myAPp\SAMLHandlingTests\bin\Debug\SSO.cer";
        X509Certificate2 cert = new X509Certificate2(certPath);
        //X509Certificate2 cert = new X509Certificate2(certPath, "LetMeIn!");


        // Open the SAML
        string samlPath = @"G:\Projects\myAPp\SAMLHandlingTests\bin\Debug\SAML.xml";

        string samlRaw = File.OpenText(samlPath).ReadToEnd();

        XmlReader rdr = XmlReader.Create(samlPath);

        List<System.IdentityModel.Tokens.SecurityToken> tokens = new List<System.IdentityModel.Tokens.SecurityToken>();

        var token = new X509SecurityToken(cert);
        tokens.Add(token);

        SecurityTokenResolver resolver = 
            SecurityTokenResolver.CreateDefaultSecurityTokenResolver(
            new System.Collections.ObjectModel.ReadOnlyCollection<SecurityToken>(tokens), true);

        //Fails on next line!
        SecurityToken securityToken = System.ServiceModel.Security.WSSecurityTokenSerializer.DefaultInstance.ReadToken(rdr, resolver);

        SamlSecurityToken deserializedSaml = securityToken as SamlSecurityToken;

问题是 XML 命名空间异常,但我不知道如何“确保将安全配置为使用指定名称、命名空间和值类型的令牌”

有人可以指出我正确的方向吗?

4

1 回答 1

2

好吧,我发现了问题,这是一个带有加密断言的 SAML 响应,它没有如下类型定义:

收到的 SAML 如下:

<saml:EncryptedAssertion>

应该是什么时候

<saml:EncryptedAssertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">

当然可以在我的代码中解决这个问题,但根本问题是 System.Xml 和 WIF 不会让我在没有完全有效的 xml 的情况下通过。

我希望这对一路上的人有所帮助。

于 2012-07-02T08:41:54.353 回答