Asp.net identity 通过中间件管道对数据库进行种子设定会引发异常asp.net核心
我的解决方案中有3层。核心层、BLL层和DAL层。除了核心层(WebAPI层)之外,所有的都是类库。DAL层包含上下文、种子、迁移、实体和存储库类。我对核心层使用ASP.NET Core 3.0预览,对类库层使用.NET standard 2.0。seeder类中的Seed方法是异步的,用于返回任务。当核心项目运行时,出现以下异常: 当我删除.Wait方法调用时,没有抛出异常,API工作正常,但数据库没有种子 我该如何解决这个问题?谢谢 Startup.cs中间件管道: 播种机等级:Asp.net identity 通过中间件管道对数据库进行种子设定会引发异常asp.net核心,asp.net-identity,asp.net-core-webapi,asp.net-core-3.0,Asp.net Identity,Asp.net Core Webapi,Asp.net Core 3.0,我的解决方案中有3层。核心层、BLL层和DAL层。除了核心层(WebAPI层)之外,所有的都是类库。DAL层包含上下文、种子、迁移、实体和存储库类。我对核心层使用ASP.NET Core 3.0预览,对类库层使用.NET standard 2.0。seeder类中的Seed方法是异步的,用于返回任务。当核心项目运行时,出现以下异常: 当我删除.Wait方法调用时,没有抛出异常,API工作正常,但数据库没有种子 我该如何解决这个问题?谢谢 Startup.cs中间件管道: 播种机等级: 您可以尝试
您可以尝试定义播种器类,如下所示:
public static class BlazeMartDbSeeder
{
public static void Seed(UserManager<ApplicationUser> userMgr, RoleManager<IdentityRole> roleMgr)
{
//var user = userMgr.FindByNameAsync("Raj");
// Add User
if (userMgr.FindByNameAsync("Raj").Result == null)
{
if (!( roleMgr.RoleExistsAsync("Manager").Result))
{
IdentityRole role = new IdentityRole();
role.Name = "Manager";
IdentityResult result = roleMgr.CreateAsync(role).Result;
IdentityResult claim=roleMgr.AddClaimAsync(role, new Claim(type: "IsManager", value: "True")).Result;
// role.Claims.Add(new IdentityRoleClaim<string>() { ClaimType = "IsManager", ClaimValue = "True" });
}
ApplicationUser user = new ApplicationUser
{
UserName = "raj",
FirstName = "Raj",
LastName = "Narayanan",
Email = "raj@raj.com"
};
IdentityResult userResult = userMgr.CreateAsync(user, "Asp3raj#").Result;
IdentityResult roleResult = userMgr.AddToRoleAsync(user, "Manager").Result;
IdentityResult claimResult = userMgr.AddClaimAsync(user, new Claim("GreenBadge", "True")).Result;
if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
{
throw new InvalidOperationException("Failed to build user and roles");
}
}
//user = userMgr.FindByNameAsync("Shawnw");
// Add User
if (userMgr.FindByNameAsync("Shawnw").Result == null)
{
if (!(roleMgr.RoleExistsAsync("Employee").Result))
{
IdentityRole role = new IdentityRole();
role.Name = "Employee";
IdentityResult result=roleMgr.CreateAsync(role).Result;
IdentityResult claim =roleMgr.AddClaimAsync(role, new Claim(type: "IsEmployee", value: "True")).Result;
// role.Claims.Add(new IdentityRoleClaim<string>() { ClaimType = "IsManager", ClaimValue = "True" });
}
ApplicationUser user = new ApplicationUser
{
UserName = "shawnw",
FirstName = "Shawn",
LastName = "Wildermuth",
Email = "shawnw@s.com"
};
IdentityResult userResult = userMgr.CreateAsync(user, "P@ssw0rd!").Result;
IdentityResult roleResult = userMgr.AddToRoleAsync(user, "Employee").Result;
IdentityResult claimResult = userMgr.AddClaimAsync(user, new Claim("OrangeBadge", "True")).Result;
if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
{
throw new InvalidOperationException("Failed to build user and roles");
}
}
// user = userMgr.FindByNameAsync("John");
// Add User
if (userMgr.FindByNameAsync("John").Result == null)
{
if (!(roleMgr.RoleExistsAsync("Vendor").Result))
{
IdentityRole role = new IdentityRole();
role.Name = "Vendor";
IdentityResult result = roleMgr.CreateAsync(role).Result;
IdentityResult claim= roleMgr.AddClaimAsync(role, new Claim(type: "IsVendor", value: "True")).Result;
// role.Claims.Add(new IdentityRoleClaim<string>() { ClaimType = "IsManager", ClaimValue = "True" });
}
ApplicationUser user = new ApplicationUser
{
UserName = "johng",
FirstName = "John",
LastName = "Galli",
Email = "gali@gali.com"
};
IdentityResult userResult = userMgr.CreateAsync(user, "P@ssw0rd!").Result;
IdentityResult roleResult = userMgr.AddToRoleAsync(user, "Vendor").Result;
IdentityResult claimResult = userMgr.AddClaimAsync(user, new Claim("BlueBadge", "True")).Result;
if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
{
throw new InvalidOperationException("Failed to build user and roles");
}
}
}
}
Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager<ApplicationUser> userMgr, RoleManager<IdentityRole> roleMgr)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
BlazeMartDbSeeder.Seed(userMgr, roleMgr);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
注意:密码的组成部分包括大小写字母、数字和特殊符号
关于ASP.NET核心标识中的种子用户和角色数据,您可以参考。这很有效。谢谢我需要指出的是,我在这一行打错了:role.Name=ManEmployeeager;。它应该是员工而不是员工经理。它抛出了一个异常,但在我纠正了错误后,一切都正常了。我编辑了你的答案以反映这一更正。
public static class BlazeMartDbSeeder
{
public static void Seed(UserManager<ApplicationUser> userMgr, RoleManager<IdentityRole> roleMgr)
{
//var user = userMgr.FindByNameAsync("Raj");
// Add User
if (userMgr.FindByNameAsync("Raj").Result == null)
{
if (!( roleMgr.RoleExistsAsync("Manager").Result))
{
IdentityRole role = new IdentityRole();
role.Name = "Manager";
IdentityResult result = roleMgr.CreateAsync(role).Result;
IdentityResult claim=roleMgr.AddClaimAsync(role, new Claim(type: "IsManager", value: "True")).Result;
// role.Claims.Add(new IdentityRoleClaim<string>() { ClaimType = "IsManager", ClaimValue = "True" });
}
ApplicationUser user = new ApplicationUser
{
UserName = "raj",
FirstName = "Raj",
LastName = "Narayanan",
Email = "raj@raj.com"
};
IdentityResult userResult = userMgr.CreateAsync(user, "Asp3raj#").Result;
IdentityResult roleResult = userMgr.AddToRoleAsync(user, "Manager").Result;
IdentityResult claimResult = userMgr.AddClaimAsync(user, new Claim("GreenBadge", "True")).Result;
if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
{
throw new InvalidOperationException("Failed to build user and roles");
}
}
//user = userMgr.FindByNameAsync("Shawnw");
// Add User
if (userMgr.FindByNameAsync("Shawnw").Result == null)
{
if (!(roleMgr.RoleExistsAsync("Employee").Result))
{
IdentityRole role = new IdentityRole();
role.Name = "Employee";
IdentityResult result=roleMgr.CreateAsync(role).Result;
IdentityResult claim =roleMgr.AddClaimAsync(role, new Claim(type: "IsEmployee", value: "True")).Result;
// role.Claims.Add(new IdentityRoleClaim<string>() { ClaimType = "IsManager", ClaimValue = "True" });
}
ApplicationUser user = new ApplicationUser
{
UserName = "shawnw",
FirstName = "Shawn",
LastName = "Wildermuth",
Email = "shawnw@s.com"
};
IdentityResult userResult = userMgr.CreateAsync(user, "P@ssw0rd!").Result;
IdentityResult roleResult = userMgr.AddToRoleAsync(user, "Employee").Result;
IdentityResult claimResult = userMgr.AddClaimAsync(user, new Claim("OrangeBadge", "True")).Result;
if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
{
throw new InvalidOperationException("Failed to build user and roles");
}
}
// user = userMgr.FindByNameAsync("John");
// Add User
if (userMgr.FindByNameAsync("John").Result == null)
{
if (!(roleMgr.RoleExistsAsync("Vendor").Result))
{
IdentityRole role = new IdentityRole();
role.Name = "Vendor";
IdentityResult result = roleMgr.CreateAsync(role).Result;
IdentityResult claim= roleMgr.AddClaimAsync(role, new Claim(type: "IsVendor", value: "True")).Result;
// role.Claims.Add(new IdentityRoleClaim<string>() { ClaimType = "IsManager", ClaimValue = "True" });
}
ApplicationUser user = new ApplicationUser
{
UserName = "johng",
FirstName = "John",
LastName = "Galli",
Email = "gali@gali.com"
};
IdentityResult userResult = userMgr.CreateAsync(user, "P@ssw0rd!").Result;
IdentityResult roleResult = userMgr.AddToRoleAsync(user, "Vendor").Result;
IdentityResult claimResult = userMgr.AddClaimAsync(user, new Claim("BlueBadge", "True")).Result;
if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
{
throw new InvalidOperationException("Failed to build user and roles");
}
}
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager<ApplicationUser> userMgr, RoleManager<IdentityRole> roleMgr)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
BlazeMartDbSeeder.Seed(userMgr, roleMgr);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}