Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a user is in a role in asp.net mvc Identity

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.

My code is:

    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 = "[email protected]" };
            userManager.Create(user, "Pa$$W0rD!");
        }

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

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

However, on the line:

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

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:

Shows User Added to DB

Shows Role Added to DB

Can anyone see what I'm doing wrong?

Thanks for any help,

Mark

like image 450
Mark Avatar asked Mar 17 '15 09:03

Mark


People also ask

What is Aspnet identity?

ASP.NET Identity is Microsoft's user management library for ASP.NET. It includes functionality such as password hashing, password validation, user storage, and claims management. It usually also comes with some basic authentication, bringing its own cookies and multi-factor authentication to the party.

How can manage user role in ASP NET MVC?

Create Roles in Asp.Net MVC Membership To create Roles, first, we need to add a Model with the name Role, and we are going to Add a Role Model inside Account Models, which is inside Models Folder. After Creating the Model, now let's add Action Method with Name “RoleCreate” for both Http Request in Account Controller.

What is MVC identity?

Identity in MVC 5 Identity is a secured way of authentication methods in web applications. It is used for identifying the authorized user. Background. There are different ways of creating an Identity in applications, but this article explains how to create it using OWIN in ASP.NET MVC.


2 Answers

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

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

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 = "[email protected]" };
        userManager.Create(user, "Pa$$W0rD!");

        // Add User To Role
        if (!userManager.IsInRole(user.Id, "Admin"))
            {
                userManager.AddToRole(user.Id, "Admin");
            }


    }

I hope that helps,

Mark

like image 60
Mark Avatar answered Oct 17 '22 15:10

Mark


Simplest thing in life;

bool isAdmin= User.IsInRole("admin") 
like image 18
Baqer Naqvi Avatar answered Oct 17 '22 15:10

Baqer Naqvi