[英]ASP.NET CORE / Issue encountered when adding .AddSignInManager<SignInManager<AppUser>>(); in the Startup class
當我嘗試在項目中添加第一個遷移時遇到的問題:
訪問 Microsoft.Extensions.Hosting 服務時出錯。 在沒有應用程序服務提供商的情況下繼續。 錯誤:無法構造某些服務(驗證服務描述符時出錯'ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.SecurityStampValidator
1[API.Entities.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator
。)(驗證服務描述符“ServiceType:Microsoft.AspNetCore”時出錯.Identity.ITwoFactorSecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[API.Entities.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[API.Entities.AppUser]'.) 無法創建“Dat”類型的 object 一個上下文'。 有關設計時支持的不同模式,請參閱https://go.microsoft.com/fwlink/?linkid=851728
class 繼承了 IdentityDbContext:
public class DataContext : IdentityDbContext<AppUser, AppRole, int,
IdentityUserClaim<int>,AppUserRole, IdentityUserLogin<int>,
IdentityRoleClaim<int>, IdentityUserToken<int>>
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
DbSet<CVUser> UserWithCV { get; set; }
//Fluent API - used to overwrite the EF Core conventions
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
然后我有AppUser,它繼承了IdentityUser
啟動 Class:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options =>
{
options.UseSqlite(_config.GetConnectionString("DefaultConnection"));
});
services.AddControllers();
//Setting up the Identity API
services.AddIdentityCore<AppUser>
(options =>
options.Password.RequireNonAlphanumeric = false
)
.AddRoles<AppRole>()
.AddRoleManager<RoleManager<AppRole>>()
.AddRoleValidator<RoleValidator<AppRole>>()
.AddEntityFrameworkStores<DataContext>()
.AddSignInManager<SignInManager<AppUser>>(); // - here is the problem
}
.AddSignInManager<SignInManager<AppUser>>();
嘗試向項目添加新遷移時,此行導致了我的問題。 知道為什么嗎?
我唯一的解決方案是刪除以下所有內容:(我猜 Identity 默認會正確創建它們) (如果我刪除 AddSignInManager 我可以創建遷移)
.AddRoles<AppRole>()
.AddRoleManager<RoleManager<AppRole>>()
.AddRoleValidator<RoleValidator<AppRole>>()
.AddSignInManager<SignInManager<AppUser>>();
你怎么看? 謝謝你。
似乎我缺少 services.AddAuthentication()。 感謝您的幫助@MartinCostello
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.