如何在 Program.cs 中调用 SeedRoles 方法
How to call SeedRoles method in Program.cs
我正在尝试为我的应用构建授权系统(基于角色的授权)。
我有种子角色 class 但我不知道在 Program.cs
到 运行 中每次当应用 运行 时调用种子方法的正确方法就像这段代码:
using login_and_registration.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace login_and_registration.Models
{
public class SeedRoles
{
public static void Initialize(IServiceProvider serviceProvider)
{
var context = serviceProvider.GetService<lAppregistrationContext>();
string[] roles = new string[] { "Owner", "Administrator", "Tests2", "Editor", "Buyer", "Business", "Seller", "Subscriber" };
foreach (string role in roles)
{
var roleStore = new RoleStore<IdentityRole>(context);
if (!context.Roles.Any(r => r.Name == role))
{
roleStore.CreateAsync(new IdentityRole(role));
}
}
context.SaveChangesAsync();
}
}
}
SeedRoles
class应该是staticclass,可以修改如下:
//required
//using Microsoft.AspNetCore.Identity;
//using Microsoft.EntityFrameworkCore;
public static class SeedRoles
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new ApplicationDbContext(serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>()))
{
string[] roles = new string[] { "Owner", "Administrator", "Tests2", "Editor", "Buyer", "Business", "Seller", "Subscriber" };
var newrolelist = new List<IdentityRole>();
foreach (string role in roles)
{
if (!context.Roles.Any(r => r.Name == role))
{
newrolelist.Add(new IdentityRole(role));
}
}
context.Roles.AddRange(newrolelist);
context.SaveChanges();
}
}
}
注意:在我的应用程序中,数据库上下文是ApplicationDbContext
,您可以将其更改为您的。
然后,在program.cs文件中,可以在var app = builder.Build();
行之后调用种子角色方法,代码如下:
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>() //enable Identity Role
.AddEntityFrameworkStores<ApplicationDbContext>() ;
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
SeedRoles.Initialize(services);
}
结果如下:
更多详细信息,您可以查看official document and sample。
我正在尝试为我的应用构建授权系统(基于角色的授权)。
我有种子角色 class 但我不知道在 Program.cs
到 运行 中每次当应用 运行 时调用种子方法的正确方法就像这段代码:
using login_and_registration.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace login_and_registration.Models
{
public class SeedRoles
{
public static void Initialize(IServiceProvider serviceProvider)
{
var context = serviceProvider.GetService<lAppregistrationContext>();
string[] roles = new string[] { "Owner", "Administrator", "Tests2", "Editor", "Buyer", "Business", "Seller", "Subscriber" };
foreach (string role in roles)
{
var roleStore = new RoleStore<IdentityRole>(context);
if (!context.Roles.Any(r => r.Name == role))
{
roleStore.CreateAsync(new IdentityRole(role));
}
}
context.SaveChangesAsync();
}
}
}
SeedRoles
class应该是staticclass,可以修改如下:
//required
//using Microsoft.AspNetCore.Identity;
//using Microsoft.EntityFrameworkCore;
public static class SeedRoles
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new ApplicationDbContext(serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>()))
{
string[] roles = new string[] { "Owner", "Administrator", "Tests2", "Editor", "Buyer", "Business", "Seller", "Subscriber" };
var newrolelist = new List<IdentityRole>();
foreach (string role in roles)
{
if (!context.Roles.Any(r => r.Name == role))
{
newrolelist.Add(new IdentityRole(role));
}
}
context.Roles.AddRange(newrolelist);
context.SaveChanges();
}
}
}
注意:在我的应用程序中,数据库上下文是ApplicationDbContext
,您可以将其更改为您的。
然后,在program.cs文件中,可以在var app = builder.Build();
行之后调用种子角色方法,代码如下:
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>() //enable Identity Role
.AddEntityFrameworkStores<ApplicationDbContext>() ;
builder.Services.AddControllersWithViews();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
SeedRoles.Initialize(services);
}
结果如下:
更多详细信息,您可以查看official document and sample。