wif - 在服务器/应用程序之间共享 IClaimsPrincipal/FedAuth Cookie ID1006

标签 wif federated-identity ws-federation

我有一个 ASP.NET 应用程序,它使用 Azure ACS(和间接 ADFS)进行身份验证 - 一切正常。现在我被要求将 SessionToken 传递给另一个后端服务,在那里可以验证它并提取声明。 [长话短说,不是我的选择]
我在解密方面很合适,而且我确定我错过了一些基本的东西。
为了设置阶段,解密时的错误是:

ID1006: The format of the data is incorrect. The encryption key length is negative: '-724221793'. The cookie may have been truncated.
ASP.NET 网站使用 RSA 包装器ala:
    void WSFederationAuthenticationModule_OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
        {
            string thumbprint = "BDE74A3EB573297C7EE79EB980B0727D73987B0D";

            X509Certificate2 certificate = GetCertificate(thumbprint);

            List<CookieTransform> sessionTransforms = new List<CookieTransform>(new CookieTransform[] 
            { 
                new DeflateCookieTransform(), 
                new RsaEncryptionCookieTransform(certificate), 
                new RsaSignatureCookieTransform(certificate) 
            });

            SessionSecurityTokenHandler sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
            e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
        }
(指纹与 FedUtil 在 web.config 中添加的值相同。
我用以下命令编写 token :
    if (Microsoft.IdentityModel.Web.FederatedAuthentication.SessionAuthenticationModule.TryReadSessionTokenFromCookie(out token))
    {
        Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler th = new Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler();
        byte[] results = th.WriteToken(token);
    ...
这给了我:
    <?xml version="1.0" encoding="utf-8"?>
    <SecurityContextToken p1:Id="_53382b9e-8c4b-490e-bfd5-de2e8c0f25fe-94C8D2D9079647B013081356972DE275" 
                      xmlns:p1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
                      xmlns="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512">
    <Identifier>urn:uuid:54bd1bd7-1110-462b-847e-7f49c1043b32</Identifier>
    <Instance>urn:uuid:0462b7d7-717e-4ce2-b942-b0d6a968355b</Instance>
    <Cookie xmlns="http://schemas.microsoft.com/ws/2006/05/security">AQAAANCMnd blah blah 1048 bytes total
    </Cookie>
    </SecurityContextToken>
并且,在另一个盒子上使用相同的证书(以及作为文件读入的 token 仅用于测试),我有:
    public static void Attempt2(FileStream fileIn, X509Certificate2 certificate, out SecurityToken theToken)
        {
            List<CookieTransform> sessionTransforms = new List<CookieTransform>(new CookieTransform[] 
            { 
                new DeflateCookieTransform(),
                new RsaSignatureCookieTransform(certificate), 
                new RsaEncryptionCookieTransform(certificate) 
            });

            SessionSecurityTokenHandler sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());

            // setup
            SecurityTokenResolver resolver;
            {
                var token = new X509SecurityToken(certificate);

                var tokens = new List<SecurityToken>() { token };

                resolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(tokens.AsReadOnly(), false);
            }

            sessionHandler.Configuration = new SecurityTokenHandlerConfiguration();
            sessionHandler.Configuration.IssuerTokenResolver = resolver;

            using (var reader = XmlReader.Create(fileIn))
            {
                theToken = sessionHandler.ReadToken(reader);
            }

        }
然后 ReadToken 抛出一个 FormatException
ID1006: The format of the data is incorrect. The encryption key length is negative: '-724221793'. The cookie may have been truncated.
在这一点上,我不知道我的整体方法是否有缺陷,或者我是否只是错过了解决所有这些问题的众所周知的“一行”。
哦,我在网站 (.NET 4.0) 上使用 VS2010 SP1,我在解码端尝试了 VS2010SP1 .NET 4.0 和 VS2012 .NET 4.5。
谢谢!

最佳答案

您的后端服务的应用程序池帐户是否具有对证书的读取访问权限?如果没有为后端服务提供对证书的读取访问权限的应用程序池帐户。由于这个原因,我过去在加密/解密方面遇到了问题。

关于wif - 在服务器/应用程序之间共享 IClaimsPrincipal/FedAuth Cookie ID1006,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13649026/

相关文章:

azure - 联合 Azure AD 时的角色声明

asp.net - 通过 ADFS 和 WIF 进行 WS-Federation 的身份验证生命周期

wcf - Azure 上 WCF REST/HTML 服务的联合身份验证(单点登录)

openid - IdM 和 SAML 的设计

amazon-web-services - 如何将 Okta 配置为 AWS Cognito 身份池的 SAML IdP?

c# - 在联合身份验证后获取用户声明并将其包含在请求 JWT 中

c# - Sharepoint 2013 中的联合身份验证 : getting rtFa and FedAuth cookies

wif - 如何使用 W.I.F/SAML token /FedAuth cookie 增加 session 超时

php - 在 PHP 上实现 Windows Identity Foundation

web-services - 如何从客户端应用程序针对 Azure Active Directory 进行身份验证?