bro*_*her 1 c# asp.net asp.net-authentication asp.net-identity
我使用它来添加一个新创建的用户,角色:
var db = new ApplicationDbContext();
ApplicationDbContext context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(user.Id, model.UserRole);
Run Code Online (Sandbox Code Playgroud)
在我的解决方案中username = email.
我发现,当用户名/电子邮件中包含一个符号(+, - 或类似的东西)时,它不会将用户添加到角色中.我得到的错误是" User name x is invalid, can only contain letters or digits.".用户成功添加,只是AddToRole,失败.
但我无法弄清楚为什么.
AllowOnlyAlphanumericUserNames 在IdentityConfig.cs中设置为false
有任何想法吗?
我能想到两种可能的解决方案.
1.)通过编辑代码示例,按照您在评论中链接的解决方案:
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
userManager.AddToRole(user.Id, model.UserRole);
Run Code Online (Sandbox Code Playgroud)
2.)重新创建UserValidator你的userManager并AddToRole再次尝试:
var db = new ApplicationDbContext();
ApplicationDbContext context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.UserValidator = new UserValidator<ApplicationUser>(userManager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true };
userManager.AddToRole(user.Id, model.UserRole);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
481 次 |
| 最近记录: |