如何在Blazor服务器端web应用程序中扩展IdentityUser



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

1.创建ApplicationUser类,从中继承IdentityUser。

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

3:更新applicationDbContext以继承自:IdentityDbContext

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

5:添加迁移迁移名称

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

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

您的属性不是公共的。

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

相关内容

  • 没有找到相关文章