【问题标题】:Checking if a user is in a role in asp.net mvc Identity检查用户是否在 asp.net mvc Identity 中的角色
【发布时间】:2015-05-19 15:57:55
【问题描述】:

我在为我的数据库添加用户和角色时遇到问题。

用户和角色都被创建了(我可以在抛出错误后在数据库中看到它们)。

但是,当我尝试检查用户是否在某个角色中时,我得到了一个异常。

我的代码是:

    public class tbInitializer<T> : DropCreateDatabaseAlways<tbContext>
    {
    protected override void Seed(tbContext context)
    {
        ApplicationDbContext userscontext = new ApplicationDbContext();
        var userStore = new UserStore<ApplicationUser>(userscontext);
        var userManager = new UserManager<ApplicationUser>(userStore);

        var roleStore = new RoleStore<IdentityRole>(userscontext);
        var roleManager = new RoleManager<IdentityRole>(roleStore);


        if(!userscontext.Users.Any(x=> x.UserName=="marktest"))
        {
            var user = new ApplicationUser { UserName = "marktest", Email = "marktest@gmail.com" };
            userManager.Create(user, "Pa$$W0rD!");
        }

        if (!roleManager.RoleExists("Admin"))
        { 
            roleManager.Create(new IdentityRole("Admin"));
        }

        if(!userManager.IsInRole("marktest","Admin"))
        { 
            userManager.AddToRole("marktest","Admin");
        }

不过,就行了:

if(!userManager.IsInRole("marktest","Admin"))

抛出异常并报错:UserId not found.

当我在抛出异常后检查时,用户和角色都在数据库中:

谁能看出我做错了什么?

感谢您的帮助,

标记

【问题讨论】:

  • 你在if(!userManager.IsInRole("marktest","Admin"))之前试过SaveChanges()吗?
  • 嗨 - 是的,我在该行之前添加了context.SaveChanges();,但仍然会引发具有相同错误的异常。谢谢,马克
  • 那么“marktest”用户是否保存在数据库中?
  • 嗨 - 是的,我已在问题中添加了屏幕截图。再次感谢,马克

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-identity


【解决方案1】:

要添加到上面 Mark 的帖子中,它在 .NET Core 3.1 中仍然几乎相同 Identity 仅与异步方法 IsInRoleAsync 不同,您必须传入 IdentityUser 类型对象:

var userInRole = await _userManager.IsInRoleAsync(user, role);

然后您可以在之后应用您的逻辑(在我的情况下,我做了两件事,首先检查角色是否实际存在,然后检查用户是否尚未分配给该角色)。

【讨论】:

    【解决方案2】:

    生活中最简单的事情;

    bool isAdmin= User.IsInRole("admin") 
    

    【讨论】:

    • 出于某种原因,这对我不起作用!我正在使用带有 JWT 身份验证的 .Net Core。
    • 如果有效,请尝试使用@if (Roles.IsUserInRole(Model.UserName, "Administrator"))
    • J86,要使用 JWT 身份验证,您必须首先在 Tokens ClaimsIdentity 中添加角色,如本文所述:stackoverflow.com/questions/42036810/…
    【解决方案3】:

    我找到了解决方案,以防其他人遇到此问题。

    “IsInRole”需要一个 User.Id - 而不是 UserName 字符串 - 所以我改为:

                if (!userManager.IsInRole(user.Id, "Admin"))
                {
                    userManager.AddToRole(user.Id, "Admin");
                }
    

    所以工作代码变成:

        ApplicationDbContext userscontext = new ApplicationDbContext();
        var userStore = new UserStore<ApplicationUser>(userscontext);
        var userManager = new UserManager<ApplicationUser>(userStore);
    
        var roleStore = new RoleStore<IdentityRole>(userscontext);
        var roleManager = new RoleManager<IdentityRole>(roleStore);
    
        // Create Role
        if (!roleManager.RoleExists("Admin"))
        { 
            roleManager.Create(new IdentityRole("Admin"));
        }
    
        if(!userscontext.Users.Any(x=> x.UserName=="marktest"))
        {
            // Create User
            var user = new ApplicationUser { UserName = "marktest", Email = "marktest@gmail.com" };
            userManager.Create(user, "Pa$$W0rD!");
    
            // Add User To Role
            if (!userManager.IsInRole(user.Id, "Admin"))
                {
                    userManager.AddToRole(user.Id, "Admin");
                }
    
    
        }
    

    希望对你有帮助

    标记

    【讨论】:

    • 我的代码看起来一样,但我仍然在同一个地方找不到 UserId
    猜你喜欢
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2019-08-22
    • 2015-05-14
    • 2016-10-24
    • 2013-11-10
    相关资源
    最近更新 更多