我正在使用 Jetbrains Rider 撰寫 ASP.Net 應用程式。我正在使用身份。我的 ApplicationDbContext 看起來像這樣:
public class BoardGameNETProjectContext : IdentityDbContext<IdentityUser>
{
public BoardGameNETProjectContext(DbContextOptions<BoardGameNETProjectContext> options)
: base(options)
{
}
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);
}
}
在Program.cs我有這一行:
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new BoardGameNETProjectContext()));
當我正在構建時,我收到了引數“選項”丟失的錯誤訊息。我不知道如何解決這個問題,我只是想添加“管理員”角色,就像在本教程中一樣:link
uj5u.com熱心網友回復:
我假設您正在使用 asp.net 6 專案 - 如果是這樣,您可以簡單地替換
var roleManager = new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(
new BoardGameNETProjectContext()));
和
// where you have this line
var app = builder.Build();
var scope = app.Services
.GetService<IServiceScopeFactory>()
?.CreateScope();
if (scope is not null)
{
using (scope)
{
var roleManager = scope
.ServiceProvider
.GetService<RoleManager<IdentityRole>>();
// .. then use the RoleManager
}
}
但您應該確保添加正確的身份:
services.AddIdentity<IdentityUser, IdentityRole>()
...而不是默認身份
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487055.html
標籤:C# 网 asp.net-mvc 骑士
