ASP.Net UserName to Email

后端 未结 7 758
北海茫月
北海茫月 2020-11-29 06:35

I am working with the new ASP.NET Identity (RTM) and I was wondering how would I go on about changing registering and login from being a UserName to an Email.

The id

7条回答
  •  青春惊慌失措
    2020-11-29 07:08

    If you really want to use e-mail address to log in, then, IMHO, the "hack" you suggested is the best approach. Because, if you insist on "doing it properly" you'll have to at least

    • modify the database schema, obviously
    • ensure username uniqueness yourself (you could make a database constraint/index do this for you, but you'll have to find a good way to deal with errors and reporting them to the user)
    • find a good substitute for just writing "User.Identity.UserName" in you code
    • probably even more

    On the other hand, if you decide to "hack" the UserName field you need to

    • modify RegisterViewModel validation (add [EmailAddress] to the UserName property), probably slightly customize [Display(Name=...)] etc.
    • make sure UserManager.UserValidator instance used in your AccountController allows special characters used in e-mail addresses. To do this, make sure its nondefault constructor looks like this:

      public AccountController(UserManager userManager)
      {
          UserManager = userManager;
          var userValidator = UserManager.UserValidator as UserValidator;
          userValidator.AllowOnlyAlphanumericUserNames = false;
      }
      

    I hope this could help you weigh the pros and cons of both approaches and come up with the best solution. Good luck!

提交回复
热议问题