AddToRole() 仅在用户电子邮件地址包含短划线时才返回“用户名只能包含字母或数字”
本文关键字:用户 包含字 返回 数字 电子邮件地址 包含短 AddToRole | 更新日期: 2023-09-27 18:30:46
我正在使用 Asp.Net Identity 2.0,配置为用户的电子邮件地址也是用户名,因此在IdentityConfig中,我在ApplicationUserManager构造函数中设置了AllowOnlyAlphanumericUserNames = false
:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// ... other options removed
}
}
我在页面中使用此代码,供员工在选中所有可用角色的 GridView 中的复选框时搜索用户并向用户帐户添加角色:
//protected UserManager<ApplicationUser> um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
//changed this to call the ApplicationUserManager instead of UserManager to make sure AllowOnlyAlphanumericUserName = false is called, but still no luck
protected ApplicationUserManager um = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
protected void cbRole_CheckChanged(object sender, EventArgs e)
{
string UserName = lblUsername.Text;
if (!String.IsNullOrEmpty(UserName))
{
CheckBox cb = (CheckBox)sender;
GridViewRow row = (GridViewRow)cb.NamingContainer;
string Role = row.Cells[1].Text;
if (cb.Checked)
{
var user = um.FindByName(UserName);
var UserResult = um.AddToRole(user.Id, Role);
if (UserResult.Succeeded)
{
lblRoles.Text = "The <b>" + Role + "</b> role has been added for " + UserName;
}
else
{
foreach (var error in UserResult.Errors)
lblRoles.Text += error; //<-- RETURNS ERROR: "User name <email address> is invalid, can only contain letters or digits." If <email address> contains a dash (-).
}
}
else
{
um.RemoveFromRole(hfAspUserID.Value, Role);
lblRoles.Text = "The <b>" + Role + "</b> role has been removed for " + UserName;
}
}
}
它通常可以完美地工作。但是,每当用户的用户名中有短划线(例如"users.name@domain-name.com")时,AddToRole() 函数都会返回错误User name users.name@domain-name.com is invalid, can only contain letters or digits.
所有帐户都是本地帐户,它未配置为使用外部登录,例如Facebook或Google。
您能提供的任何帮助将不胜感激,因为我对此问题的广泛谷歌搜索除了有关如何将电子邮件地址实现为用户名的教程之外什么都没有,我已经这样做了。
请学习并遵循此代码,它有效:
var db = new DBModel();
DBModel context = new DBModel();
var userStore = new UserStore<User>(context);
var userManager = new UserManager<User>(userStore);
userManager.UserValidator = new UserValidator<User>(userManager)
{
AllowOnlyAlphanumericUserNames = false
};
根据您的代码 AllowOnlyAlphanumericUserNames = false 仅在调用 ApplicationUserManager 的方法 'Create' 时调用。以便确保该方法在访问 um 之前调用。AddToRole() 方法。
从如下所示的上下文中获取用户管理器,然后调用您的 menthods。
var userManager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
userManager.AddToRole();
希望这有帮助。
我遇到了同样的问题 - 但是我无法访问Context
但您也可以通过 HTTP 请求获取它,所以希望这有助于其他人解决同样的问题。我使用的代码如下:
userManager = OwinContextExtensions.GetUserManager<ApplicationUserManager>(this.HttpContext.GetOwinContext());