dannyriv
dannyriv

Reputation: 87

Implementing a Custom Identity But is Crashing on StartUp

When I tried to implement a custom IdentityUser named VisUser my program was unable to start. I noticed that if I exclude the services.AddDefaultIdentity() line the program atleast starts up and reaches my index page however if I leave this uncommented out I receive this error: HTTP Error 500.30 - ANCM In-Proce ss Start Failure https://gyazo.com/907dc0b6d81ea8a9fc5a8f83f1af41ca And when I debug the code it says it crashes in my Program.cs in the line:

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

I am unsure as to why this error is happening. I am also having trouble with the migration of the new properties in my VisUser Identity, perhaps that could be the reason, but if I recall when I implemented this with the default IdentityUser this error had occurred there as well. I am using sql server as my database to store the User information.

Startup Configuration

    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));
        }

Startup Configure

    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();
        }

My 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();
        }
    }

My 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>();
            });
        }

I am unsure of what area I am doing wrong for the configuration and implementation of an IdentityUser with custom data.

Upvotes: 1

Views: 366

Answers (1)

Nan Yu
Nan Yu

Reputation: 27588

HTTP Error 500.30 - ANCM In-Process Start Failure

You should register Identity just once , either in ConfigureServices method or in IdentityHostingStartup. Otherwise you will get above error .

In addition , after using custom user entity and add new columns , you should add-migration and then update-database to update the database .

Upvotes: 1

Related Questions