【发布时间】:2018-10-28 20:55:48
【问题描述】:
我正在尝试为我的项目设置管理员用户 ID。我选择在 startup.cs 程序中执行此操作。我在 startup.cs 中添加了一个名为 CreateUsersAndRoles 的种子方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using sbingP3.Data;
using sbingP3.Models;
using sbingP3.Services;
using Microsoft.AspNetCore.Mvc;
namespace sbingP3
{
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.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//Core 2.1
services.AddIdentity<IdentityUser, IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ;
}
private async Task CreateUsersAndRoles(IServiceProvider serviceProvider)
{
//Get reference to RoleManager and UserManager from serviceProvider through dependency injection
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
//Check if admin role exists and if not add it.
var roleExist = await RoleManager.RoleExistsAsync("Admin");
if (!roleExist)
{
//create the roles and seed them to the database: Question 1
await RoleManager.CreateAsync(new IdentityRole("Admin"));
}
//Check if admin user exists and if not add it
IdentityUser user = await UserManager.FindByEmailAsync("admin@aserver.net");
if (user == null)
{
user = new IdentityUser()
{
UserName = "admin@aserver.net",
Email = "admin@aserver.net",
};
await UserManager.CreateAsync(user, "Password1!");
//Add user to admin role
await UserManager.AddToRoleAsync(user, "Admin");
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
CreateUsersAndRoles(serviceProvider).Wait();
}
}
}
我已经调试了程序,错误出现在我的种子方法中的以下行:
这个相同的代码在同一台机器上的另一个项目中成功执行,所以我认为它可能与我的项目规范有关。我尝试了各种版本的 csproj 文件,但都导致了相同的错误。
我正在使用 ASP.NET Core 2.1。
这是我的 *.csproj 文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<UserSecretsId></UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="bootstrap" Version="4.1.3" />
<PackageReference Include="bootstrap.sass" Version="4.1.3" />
<PackageReference Include="MailKit" Version="2.0.6" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.5" />
</ItemGroup>
</Project>
【问题讨论】:
-
程序在 CreateUsersAndRoles 任务的这一行中止: IdentityUser user = await UserManager.FindByEmailAsync("admin@aserver.net");
标签: asp.net-identity dbcontext asp.net-core-2.1 dbset