2
votes

enter image description here

This error occurs while trying to add-migration for the first time. I have added the extension method and my service class

public void ConfigureServices(IServiceCollection services) {

        services.AddCors();
        services.AddControllers();
        
        services.Configure<AppSettings>(AppSettings);
        services.ConfigureJWT(Configuration);
        services.ConfigureIdentity();
        services.AddScoped<ILoginService, LoginService>();
        services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, CustomClaimPrincipalFactory>();
    }

public static void ConfigureIdentity(this IServiceCollection services) {

        var builder = services.AddIdentityCore<ApplicationUser>(o =>
        {
            o.Password.RequireDigit = true;
            o.Password.RequireLowercase = false;
            o.Password.RequireUppercase = false;
            o.Password.RequireNonAlphanumeric = false;
            o.Password.RequiredLength = 6;
            o.User.RequireUniqueEmail = true;
        });
        builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole),
       builder.Services);
        builder.AddEntityFrameworkStores<AuthDBContext>()
        .AddDefaultTokenProviders();


    }

public class CustomClaimPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole> {

    public CustomClaimPrincipalFactory(
        UserManager<ApplicationUser> userManager,
        RoleManager<IdentityRole> roleManager,
        IOptions<IdentityOptions> optionsAccessor)
        : base(userManager, roleManager, optionsAccessor)
    {
    }

    protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
    {
        ClaimsIdentity identity = await base.GenerateClaimsAsync(user);
        identity.AddClaim(new Claim("ServiceSite", user.ServiceSite ?? ""));
        return identity;
    }
}

Here is the Error again

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserStore1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: AuthenticationService.Common.CustomClaimPrincipalFactory': Unable to resolve service for type 'AuthenticationService.Data.AuthDBContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'.)

1

1 Answers

8
votes

Register the dbcontext before you configure the Identity store

services.AddDbContext<AuthDBContext>();
services.AddIdentity<IdentityUser,IdentityRole>()
                .AddEntityFrameworkStores<AuthDBContext>()
                .AddDefaultTokenProviders();