gpt4 book ai didi

asp.net-mvc - SessionSecurityTokenHandler 尝试使用 DPAPI 解密 RSA 加密的 cookie 中的 SessionSecurityToken;为什么?

转载 作者:行者123 更新时间:2023-12-03 01:06:47 28 4
gpt4 key购买 nike

我在 MSDN 论坛、Dominic Baier 的博客和其他来源中读到,DPAPI 无法在 Azure 中开箱即用,并且在任何类型的 Web 场场景中处理联合身份验证的一种方法是替换DPAPI 会使用场内可用的私钥进行转换,例如使用 X509 证书的 RSA 加密。我在我的 Azure MVC 应用程序中采用了这种方法,并配置了 SessionSecurityTokenHandler,如下所示:

FederatedAuthentication.ServiceConfigurationCreated += (sender, args) =>
{
var sessionTransforms = new List<CookieTransform>(new CookieTransform[]
{
new DeflateCookieTransform(),
new RsaEncryptionCookieTransform(args.ServiceConfiguration.ServiceCertificate),
new RsaSignatureCookieTransform(args.ServiceConfiguration.ServiceCertificate)
});
var sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
args.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
};

使用此配置,我们能够从身份提供商接收 token 并发出使用这些转换加密的安全 cookie。在 Azure 模拟器中运行,一切都按预期进行。然而,在Azure环境中,我们间歇性地在浏览器中看到以下错误:

Key not valid for use in specified state.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.


Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[CryptographicException: Key not valid for use in specified state.
]
System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope) +577
Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded) +80

[InvalidOperationException: ID1073: A CryptographicException occurred when attempting to decrypt the cookie using the ProtectedData API (see inner exception for details). If you are using IIS 7.5, this could be due to the loadUserProfile setting on the Application Pool being set to false. ]
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) +862
Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver) +109
Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie) +356
Microsoft.IdentityModel.Web.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken) +123
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

这似乎表明 SessionSecurityTokenHandler 正在尝试使用 DPAPI 解密 cookie,但为什么呢?我上面不是配置为使用RSA吗?

最佳答案

请注意,您现在可以使用 MachineKeySessionSecurityTokenHandler跨网络场签署和加密 session token 。

要使用此功能,您需要删除默认的 SessionSecurityTokenHandler 并在 Web.config 中添加 MachineKeySessionSecurityTokenHandler:

<system.identityModel>
<identityConfiguration>
<securityTokenHandlers>
<remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</securityTokenHandlers>
</identityConfiguration>
</system.identityModel>

MachineKeySessionSecurityTokenHandler 使用 Web.config 中配置的机器 key ,因此您也需要添加它:

<system.web>
<machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES" />
</system.web>

请参阅 BrainThud 上的此问题

关于asp.net-mvc - SessionSecurityTokenHandler 尝试使用 DPAPI 解密 RSA 加密的 cookie 中的 SessionSecurityToken;为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904109/

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