5

I am currently adding Roles to our Database using the RoleManager with the CreateAsync(newRoleName) Method - which works correctly. But when I try to query that Role, it always returns that it doesn't exist (even though I can see it in the database).

Can anyone provide some insight on why I am not able to use the Role?

        var roleExists = roleManager.RoleExistsAsync(role);
        if (!roleExists.Result)
        {
            var newRole = new IdentityRole(role)
            {
                Name = role,
                NormalizedName = role.ToUpper(),
            };

            var roleCreated = roleManager.CreateAsync(newRole);

            Thread.Sleep(500);  // Used to get result back first.

            var roleExistsYet = roleManager.RoleExistsAsync(role);
            if (!roleExists.Result)
            {
                // ALWAYS Returns [False]
            }
        }

The initial problem came about when we were creating a new User with the UserManager, and the following method would result in an error

var roleAddResult = userManager.AddToRoleAsync(newUser, "TestRole123");

Exception Error: Role [TESTROLE123] does not exist.

Note: I can see the entry for the Role 'TestRole123' (or any other role) in the Database in the table dbo.AspNetRoles.

Any insight or help is appreciated.

Environment: Visual Studio 2017, Asp.NET Core, C#

CodingRiot
  • 135
  • 1
  • 3
  • 7

2 Answers2

2

I don't know how you declared your rolemanager, but following code works for me. It's configured in startup.cs and automatically creates a superuser if the roles haven't been created. Perhaps this can help you?

        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

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

            //superuser
            var user = new ApplicationUser
            {
                UserName = " Name",
                Email = "Email",
                Firstname = "Firstname",
                Lastname = "Lastname"
            };
            var pass = "AwesomePasswordOverHere";
            var chkUser = await userManager.CreateAsync(user, pass);

            //make superuser 
            if (chkUser.Succeeded)
            {
                await userManager.AddToRoleAsync(user.Id, "SuperUser");
            }
        }
Lotte Lemmens
  • 551
  • 5
  • 11
  • I see it now I think. AddToRoleAsync, you use the entire user, not just the Id – Lotte Lemmens Apr 27 '17 at 13:46
  • is the code that you mentioned above done in .net core or the .net full framework? The methods that you are using on the RoleManager and UserManager don't seem to be available in .net core's implementation. The implementation of the UserManager's method for AddToRoleAsync() in .net core accepts a full IdentityUser not just an id of the user unfortunately. – CodingRiot Apr 28 '17 at 15:41
  • It's not core, sorry. But doesn't core let you add packages as needed in your application. Maybe you are missing a package or perhaps update one? – Lotte Lemmens May 02 '17 at 08:26
1

One of the issues I see - you need to use keyword await in front of *Async() methods:

var roleExists = await roleManager.RoleExistsAsync(role);

and

var roleCreated = await roleManager.CreateAsync(newRole);

etc. This will remove your need to do Thread.Sleep(500); - most likely the problem is with this line.

If you can't do async methods, use non-async versions of the methods:

var roleCreated = roleManager.Create(newRole);
trailmax
  • 34,305
  • 22
  • 140
  • 234