繁体   English   中英

ASP.NET CORE / 添加时遇到的问题。AddSignInManager <signinmanager<appuser> &gt;(); 在启动 class </signinmanager<appuser>

[英]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.TwoFactorSecurityStampValidator 1[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM