44

I'm using ASP.NET Core 2.1 Identity. I've overridden IdentityUser because I need to add some additional properties on the user.

In Startup.cs

services.AddDefaultIdentity<PortalUser>().AddEntityFrameworkStores<ApplicationDbContext>();

ApplicationDbContext.cs

public partial class ApplicationDbContext : IdentityDbContext<PortalUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {

    }
}

PortalUser class

public class PortalUser : IdentityUser
{
    [PersonalData]
    public DateTime? LastLoginDateUtc { get; set; }

    [PersonalData]
    public DateTime? RegistrationDateUtc { get; set; }
}

That's all working fine. I can add a user via.

_userManager.CreateAsync(user)

However, when I call AddToRolesAsync to add a role to a user, I'm getting an exception. Any ideas why?

_userManager.AddToRolesAsync(user, new List<string> { roleName });

{System.NotSupportedException: Store does not implement IUserRoleStore<TUser>.
   at Microsoft.AspNetCore.Identity.UserManager`1.GetUserRoleStore()
   at Microsoft.AspNetCore.Identity.UserManager`1.AddToRolesAsync(TUser user, IEnumerable`1 roles)}
4
  • I've overridden IdentityUser What do you mean by this? You need to show us your code, the question is not answerable as it stands.
    – DavidG
    Commented Sep 26, 2018 at 16:40
  • Fair enough. I've edited my question.
    – John81
    Commented Sep 26, 2018 at 17:00
  • What does ApplicationDbContext inherit from?
    – DavidG
    Commented Sep 26, 2018 at 17:20
  • IdentityDbContext<PortalUser>
    – John81
    Commented Sep 26, 2018 at 17:46

3 Answers 3

116

In Startup.cs, I was missing AddRoles so

services.AddDefaultIdentity<PortalUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

should be

services.AddDefaultIdentity<PortalUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

Note: The order is critical. AddRoles must come before AddEntityFrameworkStores

1
  • 2
    You should accept this answer even if you have answered your question yourself. Commented Apr 7, 2020 at 13:39
7

For there are not any answers about the solution in asp.net Core 2.2, I would like to share the same error I meet in asp.net Core 2.2

First, here is another solution for the same error in asp.net core 2.1 https://github.com/aspnet/AspNetCore.Docs/issues/8683

And thanks to the author's idea, I meet the problem when I follow the official guidance in asp.net core 2.2 (the url is in here : MicrosoftDocs For asp.net core 2.2). When I finish the step he says and try to run the project, it throws a exception "Store does not implement IUserRoleStore"

and the problem is : actually, this is the sample for asp.net core 2.1 (And I strongly doubt that why the Microsoft will provide users a docs with not any sample codes, which can't make sense probably)

And you will find that, in Areas/Identity/Data/IdentityHostingStartup.cs IdentityHostingStartup::Configure method you have the following codes :

services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();

which is same as the code you should add in /Program.cs ConfigureService as the step : Add Role services to Identity in docs mentioned :

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();

so if you meet the same problem in asp.net core 2.2, an alternative solution is :

  1. Following the docs in asp.net 2.2
  2. When you meet this Chapter : Add Role services to Identity , just ignore the official docs and do it :

replace the row

services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();

with

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();

in Areas/Identity/Data/IdentityHostingStartup.cs IdentityHostingStartup::Configure method, but not add it in program.cs (the file can't be deleted in asp.net core 2.2)

The project I use Asp.net Identity will be updated later in my repos : UWPHelper , Good Luck :)

0

I know that the author already fixed his issue ,but I'll add this for anyone else that did all the steps in the above answer and still has this error.

From Aspnet github

You must delete autogenerated IdentityHostingStartup.Configure method in Areas/Identity/IdentityHostingStartup.cs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.