簡體   English   中英

ASP.NET MVC - 如何根據登錄用戶的角色權限隱藏或顯示鏈接/按鈕?

[英]ASP.NET MVC - How to hide or Show a link/button based on logged in User's Role permission?

我正在使用 ASP.NET MVC4。

這是我的用戶角色

1. Administrator
2. L1 Admin
3. L2 Admin

管理員組用戶有設置權限(使用添加,權限設置)。 查看日志、錯誤報告等。

如果用戶是管理員組的成員,他只能看到與上述設置相關的菜單。

我有一個菜單表,有菜單詳細信息。 有一些功能,如刪除、編輯,它們根據當前用戶的角色顯示,在頂部菜單中不可用。 列出數據時,刪除、編輯鏈接放置在表中。 這也包括對於那種類型的 entry , IsVisible 是假的。

MenuID - MenuName - Controller - Action - ParentID - IsVisible

我有一個 roleMenu 表,其中有分配給每個角色的菜單。

RoleID - MenuID

如果管理員正在登錄,他可以看到所有菜單。 如果 L1Admin 正在登錄,他只能看到分配給他的菜單。

我創建了一個用於身份驗證的自定義屬性,然后我查詢數據庫並根據控制器和操作(表菜單加入 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>

提前致謝。

編輯

我將權限詳細信息存儲在數據庫中。

例如,您可以通過以下方式進行操作:

@if (ViewContext.HttpContext.User.IsInRole("Your role"))
{
    // Do something here
}

選項 1 - 考慮到您使用的是 asp .net 會員資格。

@if (Roles.IsUserInRole("Administrator"))
{ 
  //show link 
}
else
{
  //hide link/button
}

選項 2 - 在 userData 中指定角色,以防你自己創建AuthCookie ,然后在 Global.asax.cs 文件的 Application_PostAuthenticateRequest 方法上將 HttpContext.Current.User 設置為新的 GenericPrinciple(從 authcookie 的用戶數據中獲取用戶角色) - 將實現留在你去谷歌。

這應該在以后工作

System.Web.HttpContext.Current.User.IsInRole("RoleName");

由於將權限詳細信息存儲在數據庫中,您可以通過以下方式檢查權限

Option 1創建授權操作鏈接擴展 演示

創建一個自定義的 html Authorized 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>

注意:為了更好的安全性,您需要一個自定義操作過濾器來檢查所有請求是否已授權。

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}

制作一個像這樣的自定義幫助器擴展,例如,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
    }
}
@if (User.Identity.IsAuthenticated)// check whether the user is authenticated or not
    {
        if (User.IsInRole("HR"))//Check wether the user is in that role
        {
            //Contents to be displayed for that Role!
            //some sample content which will be displayed to the user of a Role HR
            <div>
                <h5><strong>HR Approval</strong></h5>
            </div>
            <div>
                <button type="button" name="btnApprove" id="btnApprove">Approve</button>
                <button type="button" name="btnReject" id="btnReject">Reject</button>
            </div>
            <br />
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM