8

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]]'.)

4 Answers 4

31

Register the dbcontext before you configure the Identity store

services.AddDbContext<AuthDBContext>();
services.AddIdentity<IdentityUser,IdentityRole>()
                .AddEntityFrameworkStores<AuthDBContext>()
                .AddDefaultTokenProviders();
6
  • 9
    what if you don't have a dbcontext? like when using dapper instead of entity framework?
    – symbiont
    Dec 22, 2021 at 13:00
  • I have same query as @symbiont Feb 26, 2022 at 15:07
  • These will require some looking into as dapper is another use case all together. will share updates to this if I am able to make any meaningful progress on this.
    – wsduho
    Feb 27, 2022 at 16:06
  • If I change AddDefaultIdentity<1> to AddIdentity<1,2> then all the scaffolded identity pages (i.e. Account/Login) are just blank. Any Idea how this comes to be? Is scaffolding based on the content of program.cs?
    – Michael
    Sep 27, 2022 at 11:14
  • in response to those asking about when using dapper, I had this issue when I added my custom dapper class as a constructor injection and forgot to register it in program.cs using AddTransient like so, builder.Services.AddTransient<YourDapperClass>() . (Using .Net 8)
    – eaglei22
    Feb 13 at 3:40
4

For .NET Core 6.0

/*builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();*/
builder.Services.AddDbContext<ApplicationDbContext>();
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
1

in .net 7.0 and 6.0 just add the following

builder.Services.AddAuthentication();
-1

add following line above builder.Services.AddIdentity

    var connectionString =                 
    builder.Configuration.GetConnectionString("connectionstring");
    builder.Services.AddDbContext<identitycontext>(options =>
    options.UseOracle(connectionString));

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.