Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net UserName to Email

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 idea is that I want my users to sign up using their e-mail and a password (e-mail can also be acquired using external login) and they set-up a display name/username on top.

I've looked at IdentityUser and I can see that UserName is there, however since that is packed in ASP.Net Identity that can not be changed.

I know I could use 'UserName' as a e-mail, with a custom validator and then have an extra attribute for ApplicationUser called DisplayName but that is more of a hack than a solution.

I hope my question is clear. Thanks in advance.

like image 330
teh0wner Avatar asked Oct 20 '13 19:10

teh0wner


2 Answers

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<ApplicationUser> userManager)
    {
        UserManager = userManager;
        var userValidator = UserManager.UserValidator as UserValidator<ApplicationUser>;
        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!

like image 197
rootless Avatar answered Oct 22 '22 21:10

rootless


Bit late to the party, but I thought I'd throw in my $0.02.

While it's true that UserName and Email are both part of IdentityUser and thus are required, note that they are both marked as virtual. If you want UserName and Email to be the the email address, let the ApplicationUser model encapsulate the logic like so:

public class ApplicationUser : IdentityUser
{
    private string _userNameEmailBackingField;

    public override string UserName
    {
        get { return _userNameEmailBackingField; }
        set { _userNameEmailBackingField = value; }
    }

    public override string Email
    {
        get { return _userNameEmailBackingField; }
        set { _userNameEmailBackingField = value; }
    }

    //The rest of your ApplicationUser logic
}

Then in your view model, expose only a single property and map it to either/or in your ApplicationUser instance, ensuring that you decorate the view model property with [Required] and [EmailAddress] attributes.

As others have mentioned, you'll need to ensure that AllowOnlyAlphanumericUserNames is set to false for the UserManager's UserValidator, but I you get that out of the box with the newest web template in VS2013.

like image 10
joelmdev Avatar answered Oct 22 '22 23:10

joelmdev