去,你可以創建自定義的授權屬性,將比較用戶角色和角色從配置。
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