实施自定义身份,但在启动时崩溃

时间:2019-08-14 07:27:23

标签: sql-server asp.net-core

当我尝试实现名为VisUser的自定义IdentityUser时,我的程序无法启动。我注意到,如果我排除了services.AddDefaultIdentity()行,则程序至少会启动并到达我的索引页,但是,如果我不对此进行注释,则会收到此错误:HTTP错误500.30-ANCM过程中启动失败 https://gyazo.com/907dc0b6d81ea8a9fc5a8f83f1af41ca 当我调试代码时,它说它在我的Program.cs中崩溃:

CreateWebHostBuilder(args).Build().Run()

我不确定为什么会发生此错误。我在迁移VisUser Identity中的新属性时也遇到了麻烦,也许这可能是原因,但是如果我回想起当我使用默认IdentityUser实施此操作时,也会发生此错误。我正在使用sql server作为数据库来存储用户信息。

启动配置

    public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non- 
   essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });




           services.AddDbContext<dbContext>(options =>
                    options.UseSqlServer(

    Configuration.GetConnectionString("dbContextConnection")));
            /*services.AddDefaultIdentity<VisUser>()   <---It crashes at 
    startup if I uncomment this out.
                .AddDefaultUI(UIFramework.Bootstrap4)
                .AddEntityFrameworkStores<dbContext>();*/

            services.Configure<IdentityOptions>(options =>
            {
                // Password settings.
                options.Password.RequireDigit = true;
                options.Password.RequireLowercase = true;
                options.Password.RequireNonAlphanumeric = true;
                options.Password.RequireUppercase = true;
                options.Password.RequiredLength = 6;
                options.Password.RequiredUniqueChars = 1;

                // Lockout settings.
                options.Lockout.DefaultLockoutTimeSpan = 
    TimeSpan.FromMinutes(0);
                options.Lockout.MaxFailedAccessAttempts = 100;
                options.Lockout.AllowedForNewUsers = true;

                // User settings.
                options.User.AllowedUserNameCharacters =

    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
                options.User.RequireUniqueEmail = false;
            });

           services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan = TimeSpan.FromMinutes(5);

                options.LoginPath = "/Identity/Pages/Login";        //changed 
    pages from Account
                options.AccessDeniedPath = "/Identity/Account/AccessDenied";
                options.SlidingExpiration = true;
            });






    services.AddMvc().SetCompatibilityVersion
    (CompatibilityVersion.Version_2_2);

            var connection = 
    Configuration.GetConnectionString("dbContextConnection");
            services.AddDbContext<dbContext>(options => 
    options.UseSqlServer(connection));
        }

启动配置

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change 
     this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }


            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();


            app.UseMvc();
        }

我的VisUser:IdentityUser

     public class VisUser : IdentityUser
    {


        [BindProperty]
        [Required]
        public string FirstName { get; set; }

        [BindProperty]
        [Required]
        public string MiddleInitial { get; set; }

        [BindProperty]
        [Required]
        public string LastName { get; set; }

        [BindProperty]
        [Required]
        public string VisUserName { get; set; }

        [BindProperty]
        [Required]
        public string VisEmail { get; set; }

        public VisUser()
        {
            Id = Guid.NewGuid().ToString();
        }
    }

我的DatabaseContext:

    public class dbContext : IdentityDbContext<VisUser>
    {
        public dbContext(DbContextOptions<dbContext> options)
            : base(options)
        {
        }
        public DbSet<VisUser> VisUsers { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults 
    if needed.
            // For example, you can rename the ASP.NET Identity table names 
    and more.
            // Add your customizations after calling 
    base.OnModelCreating(builder);
        }
    }

IdentityHostingStartup

public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDbContext<dbContext>(options =>
                    options.UseSqlServer(
                        context.Configuration.GetConnectionString("dbContextConnection")));

                services.AddDefaultIdentity<VisUser>()
                    .AddEntityFrameworkStores<dbContext>();
            });
        }

我不确定在使用自定义数据的IdentityUser的配置和实现方面我做错了什么地方。

1 个答案:

答案 0 :(得分:1)

  

HTTP错误500.30-ANCM进程内启动失败

您应该只使用ConfigureServices方法或IdentityHostingStartup来注册一次Identity。否则,您将得到以上错误。

此外,在使用自定义用户实体并添加新列之后,您应该先add-migration,然后再update-database来更新数据库。