首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SessionSecurityTokenHandler尝试使用DPAPI解密SessionSecurityToken加密cookie中的DPAPI;为什么?

SessionSecurityTokenHandler尝试使用DPAPI解密SessionSecurityToken加密cookie中的DPAPI;为什么?
EN

Stack Overflow用户
提问于 2012-10-16 05:13:20
回答 2查看 10.2K关注 0票数 8

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

代码语言:javascript
复制
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);                    
    };

使用此配置,我们能够从身份提供者接收令牌,并发布使用这些转换加密的安全cookies。在Azure模拟器中运行,一切都按预期运行。但是,在Azure环境中,我们会间歇性地在浏览器中看到以下错误:

代码语言:javascript
复制
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吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-16 08:21:21

嗯,经过多次搜索,我找到了我的问题所在。在设置ServiceConfigurationCreated之前,我进行了一些导致访问FederatedAuthentication.ServiceConfiguration的配置。According to MSDN,“当web应用程序中的第一个HTTP模块引用ServiceConfiguration时,将引发ServiceConfigurationCreated事件”。我将事件处理程序设置移到了Application_Start的顶部,一切都正常工作,这意味着在我设置事件处理程序之前,只触发了一次事件。

希望这将节省一些人的4+小时,它花了我运行到地面。

票数 5
EN

Stack Overflow用户

发布于 2013-05-20 23:52:33

请注意,您现在可以使用MachineKeySessionSecurityTokenHandler对跨web场的会话令牌进行签名和加密。

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

代码语言:javascript
复制
<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中配置的机器密钥,因此您还需要添加该密钥:

代码语言:javascript
复制
<system.web>
  <machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES" />
</system.web>

BrainThud上查看此问题

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12904109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档