我想使用 asp.net 身份框架 创建多个用户,使用相同的电子邮件地址和不同的用户名

是否可以?

当我尝试使用相同的电子邮件地址创建用户时,出现此错误

{
  "message": "The request is invalid.",
  "modelState": {
    "": [
      "Email '[email protected]' is already taken."
    ]
  }
}

最佳答案

您可以配置 UserValidatorUserManager : 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
    };
 }

关于c# - 允许来自同一电子邮件地址的多个帐户 WEB API ASP.NET IDENTITY,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41714910/

10-13 06:45