如何在 Blazor 服务器端 Web 应用程序中扩展 IdentityUser
How to extend IdentityUser in a Blazor serverside web application
历史上,当我想在 AspNetUsers table 上添加一个或多个列时,我遵循的工作流程如下:
1.Create 一个 ApplicationUser class,从它继承 IdentityUser。
2.Add 应用程序用户的新属性 class
3:更新applicationDbContext继承自:IdentityDbContext
4:更改启动代码中对 IdentityUser 的任何引用 eg: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);
}
}
}
应用程序用户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");
});
}
}
}
你的 属性 不是 public。
public class ApplicationUser : IdentityUser
{
public string NickName { get; set; }
}
历史上,当我想在 AspNetUsers table 上添加一个或多个列时,我遵循的工作流程如下:
1.Create 一个 ApplicationUser class,从它继承 IdentityUser。
2.Add 应用程序用户的新属性 class
3:更新applicationDbContext继承自:IdentityDbContext
4:更改启动代码中对 IdentityUser 的任何引用 eg: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);
}
}
}
应用程序用户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");
});
}
}
}
你的 属性 不是 public。
public class ApplicationUser : IdentityUser
{
public string NickName { get; set; }
}