asp.net-mvc – SessionSecurityTokenHandler尝试使用DPAPI解密R
发布时间:2020-12-16 00:06:17 所属栏目:asp.Net 来源:网络整理
导读:我已经阅读过MSDN论坛,Dominic Baier的博客,以及其他来源,DPAPI将无法在Azure中开箱即用,并且在任何类型的Web场景中处理联合身份验证的一种方法是替换DPAPI转换使用可在整个服务器场中使用的私钥的服务器,例如使用X509证书的RSA加密.我在Azure MVC应用程序中
我已经阅读过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); }; 使用此配置,我们能够从身份提供商处接收令牌,并发出使用这些转换加密的安全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 在Web场中对会话令牌进行签名和加密.
要使用它,您需要删除默认的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,PublicKeyToken=b77a5c561934e089" /> </securityTokenHandlers> </identityConfiguration> </system.identityModel> MachineKeySessionSecurityTokenHandler使用在Web.config中配置的机器密钥,因此您还需要添加它: <system.web> <machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES" /> </system.web> 请参阅BrainThud的这个问题 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在预编译asp.net网站 – 是否重要?
- 在ASP.NET 5项目中运行NUnit测试(在VS Test Explorer中)
- asp.net-mvc – System.Web.Optimization.Bundle与WebEssen
- asp.net-mvc – ASP.NET MVC自定义路由搜索
- asp.net – 如何使用EF仅更新单个字段
- ASP.NET 微软Web应用示例程序走廊-项目解决方案
- asp.net – 替换过时的System.Xml.XmlDataDocument?
- 使用模块化设计组织良好的ASP.NET应用程序的最佳方法
- asp.net – 在框架4.5下添加新的Web服务
- asp.net-mvc – 如何构建URL路由?
推荐文章
站长推荐
- 在asp.net上调用两个函数按钮onclick
- asp.net-web-api – 无法在Mono / xsp4中加载类型
- asp.net-mvc – 你如何覆盖Html.ActionLink?
- Mono 3.4.1中的UserCookieAuthentication
- asp.net – 如何从c#调用javascript函数
- asp.net-mvc – Web Essentials浏览器链接在Visu
- asp.net-mvc-5 – 我应该如何从JsonResult控制器
- asp.net – 无法获取IIS的目录
- asp.net – 如何正确过滤数据表(datatable.selec
- ASP.NET WebForm的“页面生命周期”是什么?
热点阅读