How to extend IdentityUser in a Blazor serverside web application

Viewed 1178

Historically, when I have wanted to add a column or columns on to the AspNetUsers table, the workflow I would follow is as per the following:

1.Create an ApplicationUser class, inherit IdentityUser from it.

2.Add the new properties to the application user class

3:Update the applicationDbContext to inherit from: IdentityDbContext

4:Alter any references to IdentityUser in startup code eg:Startup.cs / Global etc

5:Add-Migration MigrationName

6: Update-Database

this would generate the Up/Down scripts for the new column and add the column to my database.

However, I have spun up a new Blazor serverside web-application and done the steps above to no avail.

Can anyone see anything i'm missing here, I have done this enough times in the past that I find it weird that I may have missed something, but anything is possible. hopefully someone can help, please see below for the code I have altered trying to achieve this.

Application DB context code:

namespace ExtendingBlazorIdentity.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }
}

Application User Class:

namespace ExtendingBlazorIdentity.Data
{
    public  class ApplicationUser : IdentityUser
    {
        string NickName { get; set; }
    }
}

Startup.cs

  namespace ExtendingBlazorIdentity
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<ApplicationUser>>();
            services.AddDatabaseDeveloperPageExceptionFilter();
            services.AddSingleton<WeatherForecastService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            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.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}
1 Answers

Your property is not public.

public class ApplicationUser : IdentityUser
{
   public string NickName { get; set; }
}
Related