asp.net-mvc-3 – 具有角色的AuthorizeAttribute但不对角色值进
发布时间:2020-12-16 07:32:06 所属栏目:asp.Net 来源:网络整理
导读:是否可以添加角色,但不能对以下值进行硬编码: [Authorize(Roles="members,admin")] 我想从数据库或配置文件中检索这些角色,如果我需要为Controller Action添加/删除角色,我将不需要重建应用程序. 我知道可以用枚举来做… http://www.vivienchevallier.com/A
是否可以添加角色,但不能对以下值进行硬编码:
[Authorize(Roles="members,admin")] 我想从数据库或配置文件中检索这些角色,如果我需要为Controller Action添加/删除角色,我将不需要重建应用程序. 我知道可以用枚举来做… 解决方法
您可以创建自定义授权属性,该属性将比较配置中的用户角色和角色.
public class ConfigAuthorizationAttribute: AuthorizeAttribute { private readonly IActionRoleConfigService configService; private readonly IUserRoleService roleService; private string actionName; public ConfigAuthorizationAttribute() { configService = new ActionRoleConfigService(); roleService = new UserRoleService(); } protected override void OnAuthorization(AuthorizationContext filterContext) { actionName = filterContext.ActionDescription.ActionName; base.OnAuthorization(filterContext); } protected override bool AuthorizeCore(HttpContextBase httpContext) { var availableRoles = configService.GetActionRoles(actionName); // return list of strings var userName = httpContext.User.Identity.Name; var userRoles = roleService.GetUserRoles(userName); // return list of strings return availableRoles.Any(x => userRoles.Contains(x)); } } 我希望它对你有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – ASP.NET Web API问题 – 授权/认证
- asp.net-mvc – ‘Personal’是一种类型,在给定的上下文中无
- 谈谈基于SQL Server 的Exception Handling[上篇]
- asp.net-mvc和webforms共存
- asp.net-mvc-3 – 如何在WebGrid中隐藏列?
- asp.net-mvc – 保护抓取工具中的网站内容
- asp.net-mvc – 使用MVC 5中的Entity Framework Database F
- asp.net – ASP .NET 5 MVC 6身份3角色索赔组[已关闭]
- asp.net-mvc-3 – 新加载页面而不是缓存
- 我不能在ASP.NET MVC中做的事情
推荐文章
站长推荐
热点阅读