1

This no longer works with ASP.Net Core 3.1 / .Net Core 3.1

https://stackoverflow.com/a/37173202/1698480

Compile error:'IdentityBuilder' does not contain a definition for 'AddEntityFrameworkStores' and no accessible extension method 'AddEntityFrameworkStores' accepting a first argument of type 'IdentityBuilder' could be found (are you missing a using directive or an assembly reference?) WebSite.Site C:\WorkSource....\Startup.cs 32 Active

public class ApplicationUser : IdentityUser<Guid> { }
public class Role : IdentityRole<Guid> { }

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid>
{
   ...
}

public class Startup
{
    ...
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity<ApplicationUser, Role>()
            .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
            .AddDefaultTokenProviders()
            .AddUserStore<UserStore<ApplicationUser, Role, ApplicationDbContext, Guid>>()
            .AddRoleStore<RoleStore<Role, ApplicationDbContext, Guid>>();

    }
}

If I just remove the Guid generic arg like this:

.AddEntityFrameworkStores<ApplicationDbContext, Guid>()

then I get browser error: This localhost page can’t be foundNo webpage was found for the web address: http://localhost:50827/Account/Login?ReturnUrl=%2F

How can I do this? thanks

Lukas
  • 1,699
  • 1
  • 16
  • 49
dan
  • 801
  • 15
  • 41
  • Does this answer your question? [Error CS1061 'IdentityBuilder' does not contain a definition for 'AddEntityFrameworkStores'](https://stackoverflow.com/questions/46671570/error-cs1061identitybuilder-does-not-contain-a-definition-for-addentityframe) – James P Jan 05 '20 at 00:47
  • thank you for reply, no not really, when I create UrlHelperExtensions and add the methods it can't find AccountController. added identity scaffold item and it still is not there. seems this is now using razor pages? anyway if i don't include that, I get other errors: – dan Jan 05 '20 at 21:25
  • seems like a lot has changed with asp.net core 3.1, i will go through this document: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-3.1 also perhaps use this for MVC instead of razor pages https://github.com/TanvirArjel/AspNetCoreMvcIdentity – dan Jan 05 '20 at 22:08

1 Answers1

2

This first part is just like before:

public class ApplicationUser : IdentityUser<Guid> { }
public class Role : IdentityRole<Guid> { }

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid>
{
   ...
}

And then you do the rest like this:

public class Startup
{
    … 
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity<ApplicationUser, Role>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders()
            .AddUserStore<UserStore<ApplicationUser>>()
            .AddRoleStore<RoleStore<Role>>();

    }
}

Note that ASP.NET Core 3.1 has become quite clever in figuring everything out, such as identifying that you are using GUIDs for identity. You don’t even need to specify the ApplicationDbContext type, ApplicationUser type, and Role type in your Startup; when you mention these classes, it just looks them up via reflection and figures out the details by itself.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
ali tekrar
  • 71
  • 8