ASP.net MVC - 在视图中获取当前已登录用户的角色

4

我知道这个问题已经有很多话题了,但是我已经尝试了几种解决方案,但是都不能使其正常工作。

所以,我正在尝试在我的视图中验证当前登录用户的角色。根据StackOverflow中大部分关于此问题的话题,我只需要执行以下操作:

@User.IsInRole("Admin")

很不幸,即使当前登录用户的AspNetUsers表中的“Role”列填有“Admin”,此代码始终返回false。

我还尝试了以下方法,但它显示“UserManager在当前上下文中不存在”。

<p>@UserManager.GetRoles(userId)</p>

我怀疑注册时没有正确设置用户的角色。我的AspNetRoles表已经正确填充,但是AspNetUserRoles表为空。

我该如何排除故障,找出应用程序中的问题,以便我可以使用@User.IsInRole(...)指令?

非常感谢您的帮助。


你说“即使在AspNetUsers表中的该用户的Role列是Admin”,但接着又说“AspNetUserRoles表为空”。如果有一个连接/交叉引用表,那么AspNetUsers表上就不会有一个Role列。 - krillgar
@krillgar 感谢您的回复。我不确定我是否完全理解了您的观点,但是您是在说我的AspNetUsersRoles表不应该为空吗? - newhouse-pt
1
是的。由于一个用户可以拥有多个角色,而许多用户将在同一角色中,因此AspNetUsers表上不应该有Role列。因为您的AspNetUserRoles表为空,这告诉我没有用户实际上被分配了角色。 - krillgar
你可以在检查 User.IsInRoles() 的地方设置断点,并查看用户对象上的 Roles 属性来调试此问题。我有99.9999%的把握这个集合将是空的。 - krillgar
3个回答

3

对我来说,这个工作非常完美。只需确保您已在数据库中创建了角色,同时将注册用户分配到角色中,这样就可以正常运行。

        {
            <a asp-action="StudentDashboard" asp-controller="Home">Dashboard</a>
        }
        else
        if (User.IsInRole("College"))
        {
            <a asp-action="CollegeDashboard" asp-controller="Home">Dashboard</a>
        }
        else
        if (User.IsInRole("Manager"))
        {
            <a asp-action="AdminDashboard" asp-controller="Home">Dashboard</a>
        }
        else
        if (User.IsInRole("Admin"))
        {
            <a asp-action="AdminDashboard" asp-controller="Home">Dashboard</a>
        }```



1
我认为您没有编写或者您编写的方式不正确,下面的代码是在global.asax中:
    protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
    {

        if (FormsAuthentication.CookiesSupported == true)
        {
            if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                try
                {
                    //let us take out the username now                
                    string Email = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                    string roles = string.Empty;

                    using (DatabaseContext entities = new DatabaseContext())
                    {
                        var user = entities.TblUsers.Where(u => u.Email == Email).FirstOrDefault().IDRole;

                        //here
                        roles = entities.TblRoles.Where(x => x.IDRole == user).FirstOrDefault().RoleName;

                    }
                    //let us extract the roles from our own custom cookie

                    // and here
                    //Let us set the Pricipal with our user specific details
                    e.User = new System.Security.Principal.GenericPrincipal(
                      new System.Security.Principal.GenericIdentity(Email, "Forms"), roles.Split(';'));

                }
                catch
                {

                    //somehting went wrong
                }
            }
        }
    }

0

您尝试在视图页面顶部添加以下内容:

@using Microsoft.AspNet.Identity

或者:
@if(Roles.IsUserInRole(User.Identity.GetUserName(), "Admin"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接