Asp.Net Identity save user without email

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

    ASP Identity 2.2 can be set in App_Start\IdentityConfig.cs

        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = false
        };
    
    0 讨论(0)
  • 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<StoreUser>(UserManager)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = false
    };
    
    var result = UserManager.Update(user);
    
    // restore... 
    UserManager.UserValidator = new UserValidator<StoreUser>(UserManager)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = true
    };
    
    0 讨论(0)
  • 2021-01-31 04:24

    In startup file you can disable RequireUniqueEmail by access IdentityOptions configuration Also here you can change complexity of password , Lockout,...

      services.Configure<IdentityOptions>(x =>
            {
                x.User.RequireUniqueEmail = false;
    
                //x.Password.RequireUppercase = false; => other condition for identity
    
            });
    

    you should use Microsoft.AspnetCore.Identity, address of doc here

    0 讨论(0)
  • 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
    {
      /// <summary>
      /// Used to validate an item
      /// 
      /// </summary>
      /// <typeparam name="T"/>
      public interface IIdentityValidator<in T>
      {
        /// <summary>
        /// Validate the item
        /// 
        /// </summary>
        /// <param name="item"/>
        /// <returns/>
        Task<IdentityResult> 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.

    0 讨论(0)
  • 2021-01-31 04:26

    I know this is old, but I disagree with the accepted answer, since the question is tagged as asp.net-identity-2. For the benefit of future readers, ASP.NET Identity 2.0 has a very simple solution to this problem:

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        ...snip...
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                // This disables the validation check on email addresses
                RequireUniqueEmail = false
            };
            ...snip...
        }
    }
    

    In UserValidator<TUser>, the Task<IdentityResult> ValidateAsync(T item) implementation checks this flag and determines if it should run email validation:

    if (this.RequireUniqueEmail)
    {
        await this.ValidateEmail(item, list);
    }
    

    Since you want to save users without an email address, this is how you should do it.

    CAUTION: This should only be used when email addresses are not collected. If you want to collect and validate email addresses, but make them optional during registration, you should use a custom IIdentityValidator.

    0 讨论(0)
提交回复
热议问题