Asp.Net Identity save user without email

前端 未结 5 890
旧时难觅i
旧时难觅i 2021-01-31 03:24

I want to save user without email, like this:

var user = new ApplicationUser { UserName = model.Name };
var result = await UserManager.CreateAsync(user);
         


        
5条回答
  •  滥情空心
    2021-01-31 04:21

    Actually there is a better way. Alter the validation for just when you need to ignore it and then restore it back when done. (Don't use async in this case because another thread might be need to update(?))

    // first create/update the identity!!! 
    user.PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password); //hash and update directly
    
    // alter
    UserManager.UserValidator = new UserValidator(UserManager)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = false
    };
    
    var result = UserManager.Update(user);
    
    // restore... 
    UserManager.UserValidator = new UserValidator(UserManager)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = true
    };
    

提交回复
热议问题