【问题标题】:How to extend IdentityUser in a Blazor serverside web application如何在 Blazor 服务器端 Web 应用程序中扩展 IdentityUser
【发布时间】:2021-03-04 19:53:41
【问题描述】:

从历史上看,当我想在 AspNetUsers 表中添加一列或多列时,我将遵循的工作流程如下:

1.创建一个ApplicationUser类,继承IdentityUser。

2.将新属性添加到应用程序用户类

3:更新applicationDbContext以继承自:IdentityDbContext

4:更改启动代码中对 IdentityUser 的任何引用,例如:Startup.cs / Global etc

5:Add-Migration 迁移名称

6:更新数据库

这将为新列生成 Up/Down 脚本并将该列添加到我的数据库中。

但是,我已经启动了一个新的 Blazor 服务器端 Web 应用程序,并执行了上述步骤但无济于事。

任何人都可以看到我在这里遗漏的任何东西吗?我过去已经这样做了足够多的时间了,我觉得很奇怪我可能遗漏了一些东西,但一切皆有可能。希望有人可以提供帮助,请参阅下面我为实现此目的而更改的代码。

应用数据库上下文代码:

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

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

应用程序用户类:

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

【问题讨论】:

    标签: entity-framework asp.net-core asp.net-identity blazor


    【解决方案1】:

    您的财产不公开。

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

    【讨论】:

    • 哦,我的话...正是如此,我现在感觉自己像个白痴...最近玩了很多隐含的公共记录...不敢相信我错过了! !感谢您的回答!
    • @JoshuaHolden 这是一个带有自定义 ApplicationUser 的 repo github.com/BrianLParker/AuthApp
    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2020-09-18
    • 2021-10-21
    • 1970-01-01
    • 2020-05-10
    • 2013-06-26
    相关资源
    最近更新 更多