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。不是你的选择。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – 以下部分已定义,但尚未为布局页面呈现
- ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例
- asp.net-web-api – 返回Web API中的错误
- asp.net – IE 11中的报表查看器打印按钮
- 如何利用ASP.net IIS 7.5中的浏览器缓存
- 如何处理ASP.NET vNext中的调试/发布配置转换
- asp.net – Windows Workflow Foundation的替代方案
- asp.net – NuGet:’X’已经有一个依赖定义为’Y’
- asp.net-mvc – asp.net MVC antiorgerytoken异常RedirectT
- ASP.NET将整数绑定到CheckBox的Checked字段
- asp.net-mvc-routing – 如何防止Url.RouteUrl(…
- asp.net-mvc – 覆盖控制器AuthorizeAttribute只
- asp.net-mvc – Razor MVC4 Url.Action无效
- asp.net-mvc – MVC 4 Web API – 复合键的路由
- asp.net-mvc – 无法加载文件或程序集’Microsof
- asp.net – Facebox为输入添加逗号
- asp.net – 如何将流excel文件转换为数据表C#?
- asp.net+js实现批量编码与解码的方法
- asp.net-mvc-2 – 动态生成的模型中的ASP.NET MV
- asp.net-web-api – Web Api使用IDependencyReso
