【问题标题】:Key not valid for use in specified state after IISRESETIISRESET 后密钥在指定状态下无效
【发布时间】:2014-03-16 00:48:10
【问题描述】:

我创建了一个自定义 STS 登录应用程序,登录/注销功能运行良好。但是,如果我以某些用户身份登录并执行 IISRESET 然后重新加载页面,则会吐出以下错误。 (但正如预期的那样,在我清除 cookie 并再次登录后它工作正常:))

STS 应用程序的 Apppool 用户是“A”,他从未 Windows 登录到部署 STS 应用程序的服务器计算机(他在管理员组中并有权访问加密证书)。

我尝试以“A”身份登录服务器一次(Windows 登录),然后突然间这个问题就解决了。

谁能告诉为什么它在 IISRESET 之后不工作而在 windows 登录一次后工作?

“/”应用程序中的服务器错误。

密钥在指定状态下无效

[CryptographicException: 密钥在指定状态下无效。]

System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope)
Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded)
[InvalidOperationException: 1D1073: A CryptographicException occurred when attempting to decrypt the cookie using the ProtectedData API (see inner exception for details).
Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded) +433
Microsoft.IdentityModel .Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte] cookie, Boolean outbound) +189
Microsoft. IdentityModel .Tokens. SessionSecurityTokenHandler. ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver) +894
Microsoft. IdentityModel .Tokens. SessionSecurityTokenHandler. ReadToken(Byte]] token, SecurityTokenResolver tokenResolver) +118
Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenrrocCookie(Byte] sessionCookie) +363
Microsoft.IdentityModel.Web. SessionAuthenticationModule.TryReadSessionTokenFroaCookie(SessionSecurityToken& sessionToken) +124
Microsoft. IdentityModel .Web. SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs) +61
System.Web.SyncEventExecutionstep.System.Web.HttpApplication.IExecutionStep.Execute() +80
System. Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270
Version Information: Microsoft .NET Framework Version:4.D.30319; ASP NET Version:4.O.30319.272

【问题讨论】:

    标签: encryption session-cookies wif


    【解决方案1】:

    如果您使用默认 WIF 配置运行,则用于保护会话令牌的密钥通过 DPAPI 处理并存储在用户配置文件中。除非您将 IIS 配置为“加载用户配置文件”为 true,否则这些键会在 iisreset 时更改。

    如果您在 .NET 4.5 上运行,最简单的方法是为您的应用程序配置 machineKeys 并使用新的 MachineKeySessionSecurityTokenHandler,如 Wif and Web Farms 中所述。

    【讨论】:

    • 是的,这是我发现的问题。我们通过使用基于证书的会话 cookie 加密和解密来修复它,因为我们遇到了负载平衡服务器的问题。
    • static var _CookieTransforms = new ReadOnlyCollection( new CookieTransform[] { new DeflateCookieTransform(), new RsaEncryptionCookieTransform(certificate), new RsaSignatureCookieTransform(certificate) });公共 CustomSessionSecurityTokenHandler() : base(_CookieTransforms) { }
    • 这也可以——这是 WIF 3.5 的通用解决方案。如果您在负载平衡服务器上运行,我假设您已经为应用程序指定了机器密钥。新的基于机器密钥的令牌处理程序将为您省去安装证书的麻烦。 :) 很高兴你成功了。
    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    相关资源
    最近更新 更多