我想使用 asp.net 身份框架使用相同的电子邮件地址和不同的用户名创建多个用户。
可能吗?
当我尝试使用相同的电子邮件地址创建用户时,出现此错误
{
"message": "The request is invalid.",
"modelState": {
"": [
"Email 'tester123@live.com' is already taken."
]
}
}
您可以配置UserManager
的UserValidator
:RequireUniqueEmail = false
。具有个人用户帐户的默认 MVC5 的示例代码:
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 = false
};
...
return manager;
}
您可能需要在类构造函数ApplicationUserManager
设置它:
public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
{
UserValidator = new UserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
}