检查,如果用户是在asp.net mvc的身份角色 [英] Checking if a user is in a role in asp.net mvc Identity

查看:148
本文介绍了检查,如果用户是在asp.net mvc的身份角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的播种与用户和角色数据库的问题。

I'm having an issue seeding my database with users and roles.

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

The User and the Role are both created (I can see them in the database after the error is thrown).

然而,当我尝试检查,如果用户是在一个角色,我得到一个异常。

However, when I try to check if the user is in a role, I get an exception.

我的code是:

    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");
        }

然而,就行了:

如果(!userManager.IsInRole(marktest,管理))

这是抛出异常与错误:用户ID未找到

An exception is thrown with the error: UserId not found.

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

The User and the Role are both in the database when I check after the exception is thrown:

有人能看到我在做什么错了?

Can anyone see what I'm doing wrong?

感谢您的帮助,

标记

推荐答案

我找到了解决方案,以防其他​​人是否有这个问题。

I found out the solution, in case anyone else is having this problem.

在IsInRole期待一个User.Id - 不是一个用户名字符串 - 所以我改成:

The "IsInRole" is expecting a User.Id - not a UserName string - so I changed to:

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

于是工作code变为:

So the working code becomes:

    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");
            }


    }

我希望帮助,

标记

这篇关于检查,如果用户是在asp.net mvc的身份角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆