asp.net – 服务器端声明缓存与Owin身份验证
我有一个应用程序,以前使用FormsAuthentication,一段时间,我切换使用从WindowsIdentityFramework的IdentityModel,以便我可以从基于声明的身份验证,但是它是相当丑陋的使用和实现。所以现在我在看OwinAuthentication。
我在看OwinAuthentication和Asp.Net身份框架。但是Asp.Net Identity框架的唯一实现使用EntityModel和我使用nHibernate。所以,现在我正在寻求尝试绕过Asp.Net身份,只是直接使用Owin身份验证。我终于能够得到一个工作登录使用从“How do I ignore the Identity Framework magic and just use the OWIN auth middleware to get the claims I seek?”的提示,但现在我的cookie持有索赔是相当大。当我使用IdentityModel时,我能够使用服务器端缓存机制缓存在服务器上的声明,cookie只是为缓存的信息保存一个简单的令牌。在OwinAuthentication中有类似的功能,还是我自己实现它? 我希望我会在这些船之一… > Cookie保持为3KB,哦,它有点大。 public class OwinConfiguration { public void Configuration(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Application",AuthenticationMode = AuthenticationMode.Active,CookieHttpOnly = true,CookieName = "Application",ExpireTimeSpan = TimeSpan.FromMinutes(30),LoginPath = "/Login",LogoutPath = "/Logout",ReturnUrlParameter="ReturnUrl",SlidingExpiration = true,Provider = new CookieAuthenticationProvider() { OnValidateIdentity = async context => { //handle custom caching here?? } } //CookieName = CookieAuthenticationDefaults.CookiePrefix + ExternalAuthentication.ExternalCookieName,//ExpireTimeSpan = TimeSpan.FromMinutes(5),}); } } 更新 Provider = new CookieAuthenticationProvider() { OnValidateIdentity = async context => { var userId = context.Identity.GetUserId(); //Just a simple extension method to get the ID using identity.FindFirst(x => x.Type == ClaimTypes.NameIdentifier) and account for possible NULLs if (userId == null) return; var cacheKey = "MyApplication_Claim_Roles_" + userId.ToString(); var cachedClaims = System.Web.HttpContext.Current.Cache[cacheKey] as IEnumerable<Claim>; if (cachedClaims == null) { var securityService = DependencyResolver.Current.GetService<ISecurityService>(); //My own service to get the user's roles from the database cachedClaims = securityService.GetRoles(context.Identity.Name).Select(role => new Claim(ClaimTypes.Role,role.RoleName)); System.Web.HttpContext.Current.Cache[cacheKey] = cachedClaims; } context.Identity.AddClaims(cachedClaims); } } 解决方法
OWIN cookie身份验证中间件不支持会话缓存功能。 #2不是选项。
#3是正确的方式。正如Prabu建议的,你应该在你的代码中: OnResponseSignIn: >使用唯一键(GUID)在缓存中保存context.Identity OnValidateIdentity: >从context.Identity获取唯一的键声明 我会建议你gzip的cookie,但我发现OWIN已经在它的TicketSerializer。不是你的选择。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- .net – 使用DotNetOpenAuth的localhost上的OpenID提供程序
- asp.net-mvc – Mvc3 Antiforgery令牌多标签
- ASP.Net vs MVC vs WebAPI和UseTaskFriendlySynchronizatio
- asp.net-mvc-4 – 如何为Web API控制器方法指定ContentType
- asp.net-mvc-3 – 在开发和生产之间管理EF Code First迁移
- asp.net-mvc – 绑定动作参数以在ASP.NET MVC中请求cookie
- asp.net – 在数据库表中有效地查找唯一值
- asp.net – Visual studio 2013:无法看到下拉开始使用任何
- ASP.NET页面之间传值的方式之Application实例详解
- [你必须知道的.NET] 第七回:品味类型---从通用类型系统开始
- asp.net-mvc – 当路由有多个值时如何构建RouteV
- asp.net – 在用户代码中未处理Assert faild异常
- asp.net-mvc – 如何与NopCommerce MVC合作
- asp.net-mvc – 控制台应用程序HttpClient发布到
- asp.net – Dotnet核心2.0认证多个模式身份cooki
- asp-classic – Checkbox布尔值Classic ASP
- asp.net – Visual Studio可以在新的调试会话中重
- vsdoc文件是否适用于vbscript?
- asp.net – 在javascript中读取cookie
- asp.net – Web.Config文件中的多行文本