强制其他用户使用ASP.NET Identity 2.1.0刷新其声明
发布时间:2020-12-16 07:01:50 所属栏目:asp.Net 来源:网络整理
导读:我使用的是Asp.NET Identity 2.1.0,我存储了一个用户可以访问的帐户列表,作为声明.用户登录时会生成ClaimsIdentity: public async TaskClaimsIdentity GenerateUserIdentityAsync(UserManagerApplicationUser manager){ var userIdentity = await manager.C
我使用的是Asp.NET Identity 2.1.0,我存储了一个用户可以访问的帐户列表,作为声明.用户登录时会生成ClaimsIdentity:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie); // Add Claims concerning Account userIdentity.AddClaim(new Claim("AccountList",SerializedListOfAccounts)); return userIdentity; } 假设管理员撤销用户A对特定帐户的访问权限.如何强制用户A重新生成其ClaimsIdentity?请记住,它不在用户A的上下文中.我不想等到cookie过期(并且会自动生成新的ClaimsIdentity). 可能吗?有没有办法告诉服务器将用户A的cookie视为无效并强制它重新生成? 我想要这种行为的原因是创建一个自定义AuthorizeAttribute,我可以将其放在我的控制器上,检查声明是否有用户访问,以避免额外的数据库往返. 解决方法
您不能将他们的声明存储在cookie上,而是在管道早期的每个请求中应用它们.你必须破解Startup.Auth.cs才能做到这一点.我正在做那个
here.
以下是您可以使用的要点: public partial class Startup { public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { Provider = GetMyCookieAuthenticationProvider(),// other configurations }); // other usual code } private static CookieAuthenticationProvider GetMyCookieAuthenticationProvider() { var cookieAuthenticationProvider = new CookieAuthenticationProvider(); cookieAuthenticationProvider.OnValidateIdentity = async context => { // execute default cookie validation function var cookieValidatorFunc = SecurityStampValidator.OnValidateIdentity<UserManager,ApplicationUser>( TimeSpan.FromMinutes(10),(manager,user) => { var identity = manager.GenerateUserIdentityAsync(user); return identity; }); await cookieValidatorFunc.Invoke(context); // sanity checks if (context.Identity == null || !context.Identity.IsAuthenticated) { return; } // get your claim from your DB or other source context.Identity.AddClaims(newClaim); }; return cookieAuthenticationProvider; } } 您需要在每个请求上应用声明的缺点,这可能不是非常高效.但在适当的地方适量的缓存将有所帮助.此外,这段代码并不是最容易上手的地方,因为它处于管道的早期阶段,您需要管理自己的DbContext和其他依赖项. 好处是声明会立即应用于每个用户的请求,您可以立即更改权限,而无需重新登录. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何在远程验证中使用多个AdditionalField
- asp.net – 命名Dom元素的Id属性的最佳实践
- 你所不知道的ASP.NET Core MVC/WebApi基础系列 (一)
- asp.net-mvc-4 – @ Html.Raw坚持编码引号
- 什么可能导致“客户端断开连接”的ASP.NET异常?
- asp.net – RadGrid在Visual Studio 2013中打开应用程序时,
- asp.net – 将MasterPage ImageButton事件传递给内容页面
- asp.net core 自定义异常处理中间件
- ASP.NET – Web.config登录错误找不到存储过程’dbo.aspnet
- asp.net – ServiceReference是一个自托管的WCF服务
推荐文章
站长推荐
- asp.net-mvc – 防止用户没有确认的电子邮件登录
- .net – 索引和长度必须指向字符串中的位置?
- asp.net-mvc-3 – 在Kendo网格中显示datetime字段
- 初识ABP vNext(2):ABP启动模板
- asp.net核心 – Asp.Net核心从url获取RouteData值
- 创建代码生成器可以很简单:如何通过T4模板生成代
- asp.net-mvc – @helper和Url.Action
- asp.net-mvc – ASP.NET Core MVC:设置身份cook
- asp.net-mvc – MVC4 HTML TextBox在修改ViewMod
- asp.net-mvc – 如何在ASP.NET MVC Web API中将U
热点阅读