Search code examples
c#asp.net-coreasp.net-identityasp.net-core-3.1role-manager

ASP.NET Core Identity 3.1: UserManager.RemoveFromRoleAsync always returns UserNotInRole


Im able to create roles using role manager and add user to a role using UserManager.AddToRoleAsync But when using method UserManager.RemoveFromRoleAsync(User, Role), im always getting error saying UserNotInRole

enter image description here

  • But when using UserManager.GetUsersInRoleAsync("Administrator"), I'm getting results. enter image description here

  • But again method UserManager.IsInRoleAsync(user, "Administrator"); return false.

  • Next, upon checking on the SQL profiler, I noticed that parameters set for userId & roleid is opposite during select statement of UserInRole table enter image description here

Any idea what is causing this weird behaviour? Im using Identity.Core, Version=3.1.3.0

Updated the code snippet

My AspNetUserole Table

enter image description here

Refer to below image, only methods IsInRoleAsync & RemoveFromRoleAsync get fails, other methods are getting results.

enter image description here


Solution

  • Looks like your keys for the identity UserRole table is configured wrongly. Can post the code snippet where you have modified the IdentityUserRole table?

    It should be like following configuration

    builder.Entity<IdentityUserRole<int>>().ToTable("Rmit.UserRole").HasKey(k => new {  k.UserId,k.RoleId });