[英]Asp.net identity can't add user to role
public void AddUserToRole(Guid userId, string roleName)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(DbContext));
var user = userManager.FindById(userId.ToString());
userManager.AddToRole(user.Id, roleName);
DbContext.SaveChanges();
}
I try to add a user to a role like shown above. 我尝试将用户添加到如上所示的角色。 However it does not work because when trying to go to the following controller action: 但是它不起作用,因为在尝试转到以下控制器操作时:
[AuthorizeUser(Roles = RoleEnums.UserWithProfile)]
public ActionResult Index(Guid? userProfileId)
{
}
It fails to authorize. 它没有授权。 What is strange is that it successfully manages to authorize users added in the database seeding. 奇怪的是,它成功地管理了授权在数据库中添加的用户。
private void SeedUserRoles(List<ApplicationUser> applicationUsers, DbContext dbContext)
{
var userStore = new UserStore<ApplicationUser>(dbContext);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(applicationUsers[0].Id, RoleEnums.UserWithProfile);
userManager.AddToRole(applicationUsers[1].Id, RoleEnums.UserWithProfile);
userManager.AddToRole(applicationUsers[2].Id, RoleEnums.UserWithProfile);
userManager.AddToRole(applicationUsers[3].Id, RoleEnums.User);
}
private void CreateRoles(DbContext context)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
if (!roleManager.RoleExists(RoleEnums.Admin))
{
var role = new IdentityRole { Name = RoleEnums.Admin };
roleManager.Create(role);
}
if (!roleManager.RoleExists(RoleEnums.User))
{
var role = new IdentityRole { Name = RoleEnums.User };
roleManager.Create(role);
}
if (!roleManager.RoleExists(RoleEnums.UserWithProfile))
{
var role = new IdentityRole { Name = RoleEnums.UserWithProfile };
roleManager.Create(role);
}
}
What am I missing here? 我在这里错过了什么? Is the method AddUserToRole() incorrect and why is only the seeding giving me correct behavior? 方法AddUserToRole()是否不正确,为什么只有种子给我正确的行为?
Edit: ASP.NET Identity check user roles is not working found this and it seems to be the problem here. 编辑: ASP.NET身份检查用户角色无法找到这个,这似乎是问题所在。 But i don't want users to have to manually logout and in again. 但我不希望用户必须再次手动注销。 They mention something about updating the security stamp but that did not work for me. 他们提到了有关更新安全标记的内容,但这对我不起作用。
Edit2: See my posted answer for the solution i ended up with. Edit2:看到我发布的答案,我最终得到了解决方案。
AddToRole returns an IdentityResult. AddToRole返回一个IdentityResult。 You need to check this return value for errors in the Errors collection of strings. 您需要在Errors字符串集合中检查此返回值是否存在错误。
https://msdn.microsoft.com/en-us/library/dn497483(v=vs.108).aspx https://msdn.microsoft.com/en-us/library/dn497483(v=vs.108).aspx
You should also check the return of FindById that you actually got the user. 您还应该检查实际获得用户的FindById的返回。
MVC 5 AddToRole requires logout before it works? MVC 5 AddToRole在运行之前需要注销吗?
I ended up using the solution from this question because it was the easiest solution i could find. 我最终使用了这个问题的解决方案,因为这是我能找到的最简单的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.