Search code examples
asp.net-coreasp.net-identity

How to use AddIdentity<User, Role> in dotnet core 7?


I'm using asp.net core 7 and AspNetCore.Identity 7. I wanna register identity to dependency injection system in IoC layer:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Eshop.Entities;

namespace Eshop.IoCConfig;

public static class AddCustomServicesExtension
{
    public static IServiceCollection AddCustomService(this IServiceCollection services)
    {
        // some code

        services.AddIdentity<User, Role>();

        return services;
    }
}

But there's error:

IServiceCollection does not contain a definition for AddIdentity


Solution

  • I solved this problem and use AddIdentityCore<TUser> instead of AddIdentity<TUser, TRole>:

     services.AddIdentityCore<User>()
            .AddRoles<Role>()
            .AddEntityFrameworkStores<EshopDbContext>();