【发布时间】:2019-05-04 05:15:08
【问题描述】:
我一直在阅读 dotnet core Authentication Documentation 并尝试做一些示例。我能够创建新用户,但在创建新角色时遇到了问题。当我调用 RoleManager.CreateAsync 时,它不会创建新角色。
ApplicationContext 类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<string>, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
startup.cs:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration["AppSetting:DefaultConnection"]));
services.AddIdentity<ApplicationUser, IdentityRole<string>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
我的仓库:
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signManager;
private readonly ApplicationDbContext _context;
private readonly RoleManager<IdentityRole<string>> _roleManager;
public AccountRespository(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signManager, ApplicationDbContext context, RoleManager<IdentityRole<string>> roleManager)
{
_signManager = signManager;
_userManager = userManager;
_context = context;
_roleManager = roleManager;
}
我在转发中创建角色方法
public async Task CreateRoles(string roleName)
{
await _roleManager.CreateAsync(new IdentityRole() { Name = "Admin" });
}
【问题讨论】:
-
请先检查错误信息并将其添加到问题中。必须有描述问题的错误或至少是调试消息
-
@Simonare 在调用 createAsync 之前正在处理上下文。我修好了。