C# AddToRole()返回;用户名只能包含字母或数字“;仅当用户的电子邮件地址包含破折号时
我使用的是Asp.Net Identity 2.0,配置为用户的电子邮件地址也是用户名,因此在IdentityConfig中,我在ApplicationUserManager构造函数中设置了C# AddToRole()返回;用户名只能包含字母或数字“;仅当用户的电子邮件地址包含破折号时,c#,asp.net,asp.net-identity,C#,Asp.net,Asp.net Identity,我使用的是Asp.Net Identity 2.0,配置为用户的电子邮件地址也是用户名,因此在IdentityConfig中,我在ApplicationUserManager构造函数中设置了AllowOnlyAlphanumericUserNames=false: public class ApplicationUserManager : UserManager<ApplicationUser> { public ApplicationUserManager(IUserSto
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
}
}
公共类应用程序管理员:用户管理员
{
公共应用程序服务器管理器(IUserStore存储)
:基地(商店)
{
}
公共静态应用程序SerManager创建(IdentityFactoryOptions选项,IOwinContext上下文)
{
var manager=newapplicationUserManager(newuserstore(context.Get());
//为用户名配置验证逻辑
manager.UserValidator=新的UserValidator(管理器)
{
AllowOnlyAlphanumericUserNames=false,
RequireUniqueEmail=true
};
//…删除了其他选项
}
}
我在一个页面中使用此代码,以便员工在选中所有可用角色的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;
}
}
}
//protected UserManager um=new UserManager(new UserStore(new ApplicationDbContext());
//将此更改为调用ApplicationUserManager而不是UserManager,以确保调用AllowOnlyAlphanumericUserName=false,但仍然没有成功
受保护的ApplicationUserManager um=新的ApplicationUserManager(新的用户存储(新的ApplicationDbContext());
受保护的void cbRole\u CheckChanged(对象发送方,事件参数e)
{
字符串UserName=lblUsername.Text;
如果(!String.IsNullOrEmpty(用户名))
{
复选框cb=(复选框)发送方;
GridViewRow行=(GridViewRow)cb.NamingContainer;
字符串角色=行。单元格[1]。文本;
如果(cb.选中)
{
var user=um.FindByName(用户名);
var UserResult=um.AddToRole(user.Id,Role);
if(UserResult.successed)
{
lblRoles.Text=“为“+用户名”添加了“+角色+”角色;
}
其他的
{
foreach(UserResult.Errors中的var错误)
lblRoles.Text+=error;//根据您的代码AllowOnlyAlphanumericUserNames=false仅在调用ApplicationUserManager的方法“Create”时调用。因此,请确保该方法在访问um.AddToRole()方法之前调用
从如下所示的owin上下文中获取userManager,然后调用您的方法
var userManager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
userManager.AddToRole() etc.
var userManager=Context.GetOwinContext().GetUserManager();
userManager.AddToRole()等。
希望这有帮助。我也遇到了同样的问题-但是我没有访问上下文的权限,但是您也可以通过HTTP请求获得它,所以希望这能帮助其他人解决同样的问题。我使用的代码如下:
userManager=owincontextensions.GetUserManager(this.HttpContext.GetOwinContext());
请学习并遵循此代码,它可以工作:
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
};
var db=new DBModel();
DBModel context=newdbmodel();
var userStore=新的userStore(上下文);
var userManager=newusermanager(userStore);
userManager.UserValidator=新的UserValidator(userManager)
{
AllowOnlyAlphanumericUserNames=false
};
调用cbRole\u CheckChanged方法时,您的AllowOnlyAlphanumericUserNames=false似乎未执行。您将AllowOnlyAlphanumericUserNames=false属性放置在何处,调用AddToRole()时必须已调用该属性。它在ApplicationUserManager类中…我在上面添加了它。实际上,您更新的代码也没有调用您需要的内容,我认为这已经在owin statup上调用了,因此使用该上下文获取UserManager,如下所述。