kbv*_*hnu 22 c# asp.net-mvc asp.net-mvc-3
我正在使用ASP.NET MVC4.
这是我的用户角色
1. Administrator
2. L1 Admin
3. L2 Admin
Run Code Online (Sandbox Code Playgroud)
管理员组用户具有"设置"权限(使用添加,权限设置).查看日志,错误报告等
如果用户是管理员组的成员,则他只能看到与设置相关的菜单.
我有一个菜单表,有菜单详细信息.有一些功能,如删除,编辑,这些功能是根据当前用户的角色显示的,在顶部菜单中不可用.在列出数据时,删除,编辑链接放在表中.这也包括,对于那些类型的条目,IsVisible是错误的.
MenuID - MenuName - Controller - Action - ParentID - IsVisible
Run Code Online (Sandbox Code Playgroud)
我有一个roleMenu表,有分配给每个角色的菜单.
RoleID - MenuID
Run Code Online (Sandbox Code Playgroud)
如果Admininstrator正在登录,则可以看到所有菜单.如果L1Admin正在登录,他只能看到分配给他的菜单.
我为身份验证创建了一个自定义属性,之后我查询数据库并根据Contoller和Action获取用户的权限(表Menu连接RoleMenu).因此,如果用户通过在浏览器中键入来尝试通过URL访问操作,则可以限制请求.
如果我作为L1Admin输入,我只能看到列表页面,并且菜单是相关的.在我用于列表的列表页面中.那么如何根据登录用户的权限隐藏"编辑/详细信息"链接.
<div style="float: left">
<table width="50%">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td style="width:30%;">
@Html.DisplayFor(modelItem => item.Name)
</td>
<td style="width:20%;">
// I need to hide EDIT/DELETE based on the permission setting of Current logged in user.
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
<a href="Server/@item.ID">Details</a> |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
编辑
我将权限详细信息存储在数据库中.
las*_*s88 42
例如,您可以通过以下方式执行此操作:
@if (ViewContext.HttpContext.User.IsInRole("Your role"))
{
// Do something here
}
Run Code Online (Sandbox Code Playgroud)
Rol*_*sta 12
选项1 - 考虑您使用的是asp .net成员资格.
@if (Roles.IsUserInRole("Administrator"))
{
//show link
}
else
{
//hide link/button
}
Run Code Online (Sandbox Code Playgroud)
选项2 - 在您自己创建AuthCookie的情况下指定userData中的角色,然后在Global.asax.cs文件的Application_PostAuthenticateRequest方法上将HttpContext.Current.User设置为新的GenericPrinciple(从authcookie的userdata获取userrole) - 保留实现你去谷歌.
这应该以后工作
System.Web.HttpContext.Current.User.IsInRole("RoleName");
Run Code Online (Sandbox Code Playgroud)
小智 5
制作一个像这样的自定义帮助器扩展,例如,CustomMethodForRetrievingUserFlag() 返回用户权限,CustomMethodForRetrievingFlags 返回允许的操作权限。祝你好运。
视图中的用法:@Url.CustomUrl("Home", "Index")
[Flags]
public enum AuthorizeFlags
{
Administrator = 1,
L1 = 2,
L2 = 4
}
public static class UrlHelperExtensions
{
public static MvcHtmlString CustomUrl(this UrlHelper urlHelper, string controllerName, string actionName, object routeValues = null)
{
var actionFlag = CustomMethodForRetrievingFlags(actionName);
var userFlag = CustomMethodForRetrievingUserFlag();
if ((actionFlag & userFlag) == userFlag)
{
return new MvcHtmlString(urlHelper.Action(actionName, controllerName, routeValues));
}
return new MvcHtmlString(String.Empty);
}
private static AuthorizeFlags CustomMethodForRetrievingUserFlag()
{
return AuthorizeFlags.L2;
}
private static AuthorizeFlags CustomMethodForRetrievingFlags(string actionName)
{
return (AuthorizeFlags.Administrator | AuthorizeFlags.L1); // test stub
}
}
Run Code Online (Sandbox Code Playgroud)
由于将权限详细信息存储在数据库中,您可以通过以下方式检查权限
Option 1 创建授权的操作链接扩展.演示
创建自定义html授权ActionLink并调用如下
<ul id="menu">
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<li><%: Html.ActionLink("About", "About", "Home")%></li>
// Next line What you are looking for
<li><%: Html.ActionLinkAuthorized("The Privilege Zone", "ThePrivilegeZone", "Home", true)%></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
注意:为了更好的安全性,您需要一个自定义操作过滤器来检查所有请求是否已获得授权.
Option 2
创建静态函数并检查操作链接之前
public static bool IsUserInRole(string rolenamefrom session)
{
// Check the user have the privilege then return true/false
}
@if (IsUserInRole("Administrator"))
{ //show link }
else
{//hide link/button}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66965 次 |
| 最近记录: |