去,你可以创建自定义的授权属性,将比较用户角色和角色从配置。
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));
}
}
我希望它可以帮助你。
要明确,在我的情况下,组将是角色和角色将是功能。因此,使用我的术语,每个角色都有许多功能,一对多的关系:角色(1) - 功能(*)。我的计划是使用Action Name,就像你在'CreateUser'属性中完成的一样。在这种情况下,任何具有创建用户功能的角色都将被允许。 – 2012-01-28 19:11:11
@zebula这是正确的。 – Eranga 2012-01-29 00:22:51