想象一下,你有服务基于控制器和动作名称这样它返回角色的数组:
public class RoleProvider
{
public string[] Get(string controller, string action)
{
// get your roles based on the controller and the action name
// wherever you want such as db
// I hardcoded for the sake of simplicity
return new string[]{"Student", "Teacher"};
}
}
现在,您可以编写自己的授权属性是这样的:
public class DynamicRoleAuthorizeAttribute: AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var controller = httpContext.Request.RequestContext
.RouteData.GetRequiredString("controller");
var action = httpContext.Request.RequestContext
.RouteData.GetRequiredString("action");
// feed the roles here
Roles = string.Join("," ,_rolesProvider.Get(controller, action));
return base.AuthorizeCore(httpContext);
}
}
现在使用您的自定义授权属性而不是像这样的旧版本:
[DynamicRoleAuthorize]
public ActionResult MyAction()
{
}
您是否已经在项目中使用角色? – SeM
这里我从AspNetRole表中检索了角色。我种下这个桌子有四个角色。但我担心的是,我可以如何让管理员将操作方法的权限从UI分配给角色。此用户界面用于演示目的。我想要像这样创建。 –
我已经问过这个问题,知道您是使用分配角色还是使用否的部分,所以如果是的话,当您检查/取消选中复选框时,您只需要使用该部分。 – SeM