Reputation: 111
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.UserClaimsPrincipalFactory
1[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.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1[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.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserStore1[AuthenticationService.Entities.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[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.UserStore
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore
1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
1[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.RoleStore
5[Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[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.UserStore
9[AuthenticationService.Entities.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,AuthenticationService.Data.AuthDBContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'.)
Upvotes: 10
Views: 37851
Reputation: 1
I've had your problem and I've had two separate Projects and I wasn't specified were to store the Entities in DbConext
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityDbContext>().AddDefaultTokenProviders();
and adding AddEntityFrameWorkStores() solved my problem
Upvotes: -1
Reputation: 1
.NET 8
builder.Services.AddDataProtection(); // 数据保护 Identity 需要
builder.Services.AddIdentityCore<MyUser>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider;
options.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;
});
Upvotes: 0
Reputation: 59
add following line above builder.Services.AddIdentity
var connectionString =
builder.Configuration.GetConnectionString("connectionstring");
builder.Services.AddDbContext<identitycontext>(options =>
options.UseOracle(connectionString));
Upvotes: -1
Reputation: 891
in .net 7.0 and 6.0 just add the following
builder.Services.AddAuthentication();
Upvotes: 4
Reputation: 312
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();
Upvotes: 4
Reputation: 477
Register the dbcontext before you configure the Identity store
services.AddDbContext<AuthDBContext>();
services.AddIdentity<IdentityUser,IdentityRole>()
.AddEntityFrameworkStores<AuthDBContext>()
.AddDefaultTokenProviders();
Upvotes: 36