ASP.Net Core 全新。必须使用 Identity 创建一个 asp.net core 2.2 项目(并让用户播种)。
我找不到任何有关如何准确执行此操作的文档。
我能够找到创建身份角色的代码(无论如何编译,还没有达到可以运行它的位置:
private static async Task CreateUserTypes(ApplicationDbContext authContext, IServiceProvider serviceProvider)
{
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
string[] roleNames = { "Administrator", "Data Manager", "Interviewer", "Respondent" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
var roleExist = await RoleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
}
}
}
现在,我需要创建一些用户。但是我找不到奇怪的微软语法(谷歌搜索了两天)。
这是做什么的not work:
private static async Task CreateRootUser(Models.CensORContext context, ApplicationDbContext authContext, IServiceProvider serviceProvider)
{
//Create the root ADMIN user for all things admin.
UserManager<ApplicationDbContext> userManager = serviceProvider.GetRequiredService<UserManager<ApplicationDbContext>>();
IdentityUser user = new IdentityUser()
{
UserName = "[email protected] /cdn-cgi/l/email-protection",
Email = "[email protected] /cdn-cgi/l/email-protection"
};
var NewAdmin = await userManager.CreateAsync(user, "password");
}
我看到的错误是:
Argument1:无法从“Microsoft.AspNetCore.Identity.IdentityUser”转换为“ApplicationDbContext”
是这个意思吗?显然,我没有合适的用户管理器。但,我怎样才能找到合适的将用户作为第一个参数,将字符串(密码)作为第二个参数?
此外,谷歌搜索中出现的例子有一个ApplicationUser
我没有(并且不需要?)的对象。示例中没有定义我如何获取它。
Owen
好的。过去有语法错误,但现在我收到运行时错误:
NullReferenceException:未将对象引用设置为对象的实例。在调用 CreateAsync 时。这是新代码:
private static async Task CreateRootUser(Models.CensORContext context, ApplicationDbContext authContext, IServiceProvider serviceProvider)
{
//Create the root ADMIN user for all things admin.
var userStore = new UserStore<IdentityUser>(authContext);
UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(userStore, null, null, null, null, null, null, serviceProvider, null);
// = serviceProvider.GetRequiredService<UserManager<ApplicationDbContext>>();
IdentityUser user = new IdentityUser()
{
UserName = "[email protected] /cdn-cgi/l/email-protection",
Email = "[email protected] /cdn-cgi/l/email-protection"
};
var result = await userManager.CreateAsync(user, "password");
}
是否要研究 create userManager 的其他参数是什么以及如何从 serviceProvider 获取它们?
--Owen
想出了怎么做。关键是找到正确的服务提供商来传递and创建 userManager 的正确语法。我通过谷歌找到的其他答案更换IdentityUser
与他们自己的ApplicationUser
那是把水搅浑了。这是工作功能(希望这对某人有帮助):
private static async Task CreateRootUser(Models.CensORContext context, ApplicationDbContext authContext, IServiceProvider serviceProvider)
{
//Create the root ADMIN user for all things admin.
var userStore = new UserStore<IdentityUser>(authContext);
UserManager<IdentityUser> userManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
//new UserManager<IdentityUser>(userStore, null, null, null, null, null, null, serviceProvider, null);
// = serviceProvider.GetRequiredService<UserManager<ApplicationDbContext>>();
IdentityUser user = new IdentityUser()
{
UserName = "[email protected] /cdn-cgi/l/email-protection",
Email = "[email protected] /cdn-cgi/l/email-protection"
};
var result = await userManager.CreateAsync(user, "password");
result = await userManager.AddToRoleAsync(user, "Administrator");
}