ASP.Net MVC 5 Identity roles added to database, but User.IsInRole always returns false

I have an ASP.Net MVC 5 application using a new authentication model. Everything seemed to work, but the collection of roles for users always does not contain elements, and User.IsInRole always returns false.

I can successfully add roles to users using UserManager.AddToRole(identity, "Admin");

This gives: http://imgur.com/vobtgO2

The model looks right: http://imgur.com/0csxrZm

I tried to force Lazy Loading in the Database Context class, but that didn't make any difference.

Are there any additional configuration steps that I need to perform to associate roles with users? I use the IdentityRole class, and my standard ApplicationUser class inherits IdentityUser.

Any help would be greatly appreciated as I have spent too much time on this already.

+4
source share
2 answers

Turns out this was the problem of Lazy Loading missing, even though I set LazyLoadingEnabled to true in the class constructor.

In the onmodelcreating method, you must enable Lazy Loading:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        this.Configuration.LazyLoadingEnabled = true;

Thank you for all of your guys.

+2
source

, - roleManager.Create(new IdentityRole("Admin")); roleManager.Create(new IdentityRole("NonAdmin"));? , .

0

Source: https://habr.com/ru/post/1535402/


All Articles