[英]Custom authorization in Razor view
我目前正在从事C#MVC项目。 我使用CustomAuthorizationAttribute
来授权项目中的每个用户。
我有3个角色:超级管理员,管理员和用户
创建新用户时,可以选择添加相应的角色,并且可以对其进行编辑(具有“用户”角色的人不能添加或编辑用户)。
为了设置此身份验证,我分别创建了两个表“ PermissionFunction ”和“ Permission ”。 该表的详细信息如下:
PermissionFunction :
|---------------------|------------------|
| PermFunc_ID | bigint |
|---------------------|------------------|
| PermFunc_Name | varchar(50) |
|---------------------|------------------|
权限 :
|---------------------|------------------|
| Perm_ID | bigint |
|---------------------|------------------|
| Perm_RollID | bigint |
|---------------------|------------------|
| Perm_PermFuncID | bigint |
|---------------------|------------------|
PermFunc_ID和Perm_ID分别是表的主键。 PermFunc_Name是引用所有控制器中每个动作的名称。 权限表包含Role和PermissionFunction表的外键。
供参考的是我的角色表:
|---------------------|------------------|
| Rol_ID | bigint |
|---------------------|------------------|
| Rol_Name | varchar(50) |
|---------------------|------------------|
为了进行身份验证,我添加了一个类CustomAuthorizationAttribute并将授权属性添加到每个控制器操作中。
例如,考虑我的PermissionFunction表值,如下所示:
|---------------------|-------------------|
| PermFunc_ID | PermFunc_Name |
|---------------------|-------------------|
| 1 | userIndex |
|---------------------|-------------------|
| 2 | userCreate |
|---------------------|-------------------|
和我的HomeController :
public class UserController : Controller
{
[CustomAuthorization(IdentityRoles = "userIndex")]
public ActionResult Index()
{
return View();
}
[CustomAuthorization(IdentityRoles = "userCreate")]
public ActionResult Create()
{
return View();
}
}
和CustomAuthorizationAttribute类:
public class CustomAuthorizationAttribute : AuthorizeAttribute
{
private PermissionRepository _permission = new PermissionRepository();
private PermissionFuncRepository _permissionFun = new PermissionFuncRepository();
// roles start
public string IdentityRoles
{
get { return _permissionName ?? String.Empty; }
set { _permissionName = value; }
}
private string _permissionName;
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//do the base class AuthorizeCore first
if (httpContext.User.Identity.IsAuthenticated)
{
string RoleID = FormsAuthentication.Decrypt(httpContext.Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name.Split('|')[1];
var permisionID = _permissionFun.FindByName(_permissionName);
if(permisionID != null)
{
var permis = _permission.GetPermission().Where(a => a.Perm_PermFuncID == permisionID.PermFunc_ID && a.Perm_RollID.ToString() == RoleID).FirstOrDefault();
if (permis != null)
{
return true;
}
}
}
return false;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
//if the user is not logged in use the deafult HandleUnauthorizedRequest and redirect to the login page
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
//if the user is logged in but is trying to access a page he/she doesn't have the right for show the access denied page
else
{
// the controller action was invoked with an AJAX request
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new RedirectResult("~/Home/AccessDeniedNew");
}
else
{
filterContext.Result = new RedirectResult("~/Home/AccessDenied");
}
}
}
}
一切正常。
这是我的索引 html:
<button class="btn btn-sm btn-rounded btn-success" type="button" id ="CreateButton" onclick="UserCreate()"> Create New User </button> // create new user ..... // code continues // code for modal popup <div id="edit-user" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> </div> <script type="text/javascript"> function UserCreate() { var url = "/User/Create/"; $.get(url, function (data) { $('#edit-user').html(data); $('#edit-user').modal('show'); }); } </script>
单击CreateButton时 ,将弹出一个模态弹出窗口,用于添加详细信息并创建新用户。
如果用户无权创建新用户,有什么办法可以隐藏“创建新”按钮? (即:如果userCreate不在PermissionFunction表中)。
您可以检查权限,并在需要时向页面添加按钮。 您已经知道获取权限信息的方法,因此可以将一些信息存储在ViewData
或ViewBag
并检查是否应存在创建按钮。 该信息可以只是一个简单的boolean
。 会是这样的
@if(ViewBag.CanCreate)
{
<button class="btn btn-sm btn-rounded btn-success" type="button" id ="CreateButton" onclick="UserCreate()"> Create New User </button>
}
您可以在控制器上设置CanCreate
。
我宁愿建议你从这个话题
但是解决方案不是很好,您可以创建自己的操作链接,例如
@html.AuthorizeActionLink("button name","your action/controller name","your security role")
我认为您甚至可以摆脱最后一个参数,并自动检查连接的用户以及您要选择显示或不显示的操作所要求的权限。
如果您确实需要特定的html,那么我建议在此线程中使用解决方案。如果您要在一页上处理多个访问权限,则会更容易。 确实,如果您要管理许多Viewbag,将开始感到痛苦。
希望这可以帮助
问候
编辑:对于FPGA实现的authorizeActionLink我发现这
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.