我正在用MVC4编写一个定制的web系统,该系统的一部分要求管理员用户管理角色,而公司的用户需要为系统的某些领域提供访问和权限。该系统有以下模块:
销售生产
管理团队希望能够在系统中创建角色并对这些角色应用权限。例如,销售角色将被拒绝访问生产,但是销售经理可以只读取对生产的访问。
我正在寻找一个例子,最好的方法来管理这个单一的管理屏幕。管理员需要
此外,在需要动态分配角色时,我将如何在Controller级别实现它?
[Authorize(Roles="Sales")] // needs to be dynamic
public ActionResult SalesIndex(){
return View();
}
如有任何意见,将不胜感激。
谢谢
发布于 2013-03-11 13:38:58
您需要创建这样的自定义AuthorizeAttribute
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var userIdentity = httpContext.User.Identity;
if (!userIdentity.IsAuthenticated)
return false;
var rd = httpContext.Request.RequestContext.RouteData;
string currentAction = rd.GetRequiredString("action");
if(currentAction == "SalesIndex")
{
return IsUserIsInRoleForTheView(userIdentity.Name);
}
return true;
}
}
[CustomAuthorize]
public ActionResult SalesIndex()
{
return View();
}
发布于 2013-09-29 17:42:09
要做到这一点,一种方法是有一个具有两个角色级别的数据模型:
然后,您将有一个定制的管理UI,将用户分配给GroupRoles,GroupRoles分配给PermissionRoles。
您还将有一个自定义RoleProvider,它“扁平”这个模型,即GetRolesForUser
方法为用户返回所有PermissionRoles (通过他的GroupRole成员资格)。
然后,您可以使用标准的.NET API进行授权,并且不需要定制的授权属性:
[Authorize(Roles="SomeAction")] // for an MVC Controller
[PrincipalPermission(SecurityAction.Demand, Role = "SomeAction")] // For a method in the BLL
Thread.CurrentPrincipal.IsInRole("SomeAction") // in code
https://stackoverflow.com/questions/15339771
复制相似问题