【问题标题】:User.IsInRole("Admin") not working Identity 2.0User.IsInRole("Admin") 不工作 Identity 2.0
【发布时间】:2016-11-22 16:48:33
【问题描述】:

我在我的 Razor _layout.cshtml 页面中使用 User.IsInRole("Admin") 来检查经过身份验证的用户的角色并相应地显示菜单。以下是我正在使用的代码:

@if (Request.IsAuthenticated && User.IsInRole("Admin"))
            {
                <li class="@Html.RouteIf("index", "active")">
                    <a href="@Url.Action("Dashboard", "home")" title="Dashboard"><i class="fa fa-lg fa-fw fa-dashboard"></i><span class="menu-item-parent">Analytics Dashboard</span></a>
                </li>
}

现在的问题是管理员已成功登录系统,但菜单中没有显示链接。

在谷歌搜索这个问题时,我在 web.config 中进行了以下更改

<system.web>
    <roleManager enabled="true" />
  </system.web>

<remove name="RoleManager" />

感谢任何帮助!

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-identity-2


    【解决方案1】:

    而不是

    @if (Request.IsAuthenticated && User.IsInRole("Supervisor"))
    {
    }
    

    我用

    部分视图 在 _Layout.cshtml

    <li>@Html.Partial("_SupervisorPartial")</li>
    

    现在可以正常使用了。

    @using Microsoft.AspNet.Identity
    @using System.Web.Security;
    @if (Request.IsAuthenticated)
    {
        var manager = new UserManager<IdentitySample.Models.ApplicationUser> (new Microsoft.AspNet.Identity.EntityFramework.UserStore<IdentitySample.Models.ApplicationUser> (new IdentitySample.Models.ApplicationDbContext()));
        var user = manager.FindById(User.Identity.GetUserId());
        if (user != null && manager.IsInRole(user.Id, "Supervisor"))
        {
            using (Html.BeginForm())
            { //your code
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-30
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      相关资源
      最近更新 更多