Asp.net core 在.net核心框架中添加具有管理员角色的超级用户
目前我正在尝试在ASP.Net核心web应用程序中添加一个具有管理员角色的超级用户。 我想在启动时添加这个用户,我花了一些时间研究这个主题,但没有成功。 sartup看起来非常标准,开箱即用,如下所示Asp.net core 在.net核心框架中添加具有管理员角色的超级用户,asp.net-core,Asp.net Core,目前我正在尝试在ASP.Net核心web应用程序中添加一个具有管理员角色的超级用户。 我想在启动时添加这个用户,我花了一些时间研究这个主题,但没有成功。 sartup看起来非常标准,开箱即用,如下所示 public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(e
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseIdentity();
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
公共类启动
{
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true);
if(env.IsDevelopment())
{
//有关使用用户机密存储的更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
//这将使遥测数据更快地通过Application Insights管道,从而允许您立即查看结果。
AddApplicationInsightsSettings(developerMode:true);
}
builder.AddEnvironmentVariables();
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddApplicationInsightsTelemetry(配置);
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseIdentity();
//在下面添加外部身份验证中间件。要配置它们,请参阅http://go.microsoft.com/fwlink/?LinkID=532715
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
如何实现这一点?将参数
ApplicationDbContext dbContext
添加到Configure
方法中-依赖项注入将创建适当的对象,您可以找到/添加所需的用户:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory, ApplicationDbContext dbContext)
{
...
if (dbContext.Users.Find(x => Name == "superadmin") == null)
{
db.Users.Add(new User { Name = "superadmin", ... });
db.SaveChanges();
}
}
或者,您可以将
UserManager
添加到参数中,并将其用于用户操作。启动类不适合这样做,因为您需要用户首先登录。处理此问题的最佳方法是在AccountController中的登录操作。您好,谢谢您的建议。启动时我需要它,因为此用户应该是超级用户用户和管理员知道,它应该已经存在于数据库中,如果不存在,则添加到数据库中。因此,当您登录时,您使用此用户。您可以给出一个如何实现此目的的示例吗?谢谢您的建议。我需要添加一个具有管理员权限的用户,该用户将为管理员所知,并且应进行硬编码;您的代码只添加一个用途r、 。我提供了一种在应用程序启动期间向数据库添加数据的方法。您可以添加任何数据,包括角色、声明等,具体取决于您的需要。事实上,该方法非常有效。谢谢您的提示。