【发布时间】:2018-08-10 00:57:41
【问题描述】:
使用 VS2017 内置的模板,用于带有身份验证的 Core 2 MVC。尝试使用用户和角色为数据库播种。角色运作良好,他们播种。用户总是返回一个错误说“不能为空”,我认为这是因为初始化函数末尾的这一行实际上找不到用户
await _userManager.AddToRoleAsync(await _userManager.FindByNameAsync(user), "Administrator");
我使用这篇文章来构建它: https://gist.github.com/mombrea/9a49716841254ab1d2dabd49144ec092
注意 - 这篇文章是针对 Core 1.0 的,而我正在使用 Core 2.0,这可能就是我遇到问题的原因。
如前所述,我可以播种角色,但用户不会自己创建。一旦我弄清楚这一点,我将尝试多个用户,我已经进行了创建多个角色的测试。
这里是初始化类
//Taken from :
//https://gist.github.com/mombrea/9a49716841254ab1d2dabd49144ec092
public interface IdentityDBIitializer
{
void Initialize();
}
public class LatestUserDBInitializer : IdentityDBIitializer
{
private readonly ApplicationDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public LatestUserDBInitializer(
ApplicationDbContext context,
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager)
{
_context = context;
_userManager = userManager;
_roleManager = roleManager;
}
//This example just creates an Administrator role and one Admin users
public async void Initialize()
{
//create database schema if none exists
_context.Database.EnsureCreated();
//If there is already an Administrator role, abort
//if (_context.Roles.Any(r => r.Name == "Administrator")) return;
//Create the Administartor Role
//await _roleManager.CreateAsync(new IdentityRole("Administrator"));
//Create the default Admin account and apply the Administrator role
string user = "me@myemail.com";
string password = "z0mgchangethis";
await _userManager.CreateAsync(new ApplicationUser { UserName = user, Email = user, EmailConfirmed = true }, password);
await _userManager.AddToRoleAsync(await _userManager.FindByNameAsync(user), "Administrator");
}
}
这是我的启动:
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<POSContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UKFest2018Context")));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UKFestIdentityContext")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add Database Initializer
services.AddScoped<IdentityDBIitializer, LatestUserDBInitializer>();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
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, IdentityDBIitializer dBInitializer)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
//Seeds Identity DB
dBInitializer.Initialize();
//UserRoleInitializer.InitializeUsersAndRoles(app.ApplicationServices);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
【问题讨论】:
-
ApplicationUser中的一个字段不应为空,因此Cannot be NULL可能需要查看哪个字段是必需的。 -
试试这个密码,
P@ssw0rd!或者你可以在你的启动中设置自定义用户选项来获得你想要的任何类型的密码。
标签: .net-core asp.net-core-mvc asp.net-core-2.0