Asp.Net Identity save user without email

前端 未结 5 883
旧时难觅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:26

    Identity depends on email as a way to reset user password.

    However, ignoring email is not simple, but possible. You'll need to implement Microsoft.AspNet.Identity.IIdentityValidator interface that ignores the lack of email:

    namespace Microsoft.AspNet.Identity
    {
      /// 
      /// Used to validate an item
      /// 
      /// 
      /// 
      public interface IIdentityValidator
      {
        /// 
        /// Validate the item
        /// 
        /// 
        /// 
        /// 
        Task ValidateAsync(T item);
      }
    }
    

    And then in ApplicationUserManager assign your own implementation to property UserValidator.

    If you really need this, you can get the original source code for UserValidator by decompiling Microsoft.AspNet.Identity.UserValidator class and peeking into the existing source code and removing cheecking for email.

    However, I'm not sure how the rest of the framework will react on lack of email on user. Probably you'll get exceptions in other operations.

提交回复
热议问题