ASP.Net UserName to Email

后端 未结 7 704
北海茫月
北海茫月 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:04

    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.

    0 讨论(0)
  • 2020-11-29 07:04

    While working on a similar problem, I found the easiest solution is simply to label the Username input as "Email" and then manually set the Membership email during onCreating.

    protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e){
        RegisterUser.Email = RegisterUser.UserName;            
    }
    
    0 讨论(0)
  • 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<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!

    0 讨论(0)
  • 2020-11-29 07:10

    I get this working.

    First go to accountViewModels and add a Property for the UserName

    Public Class RegisterViewModel
        <Required>
        <Display(Name:="User Name")>
        Public Property UserName As String
    
        <Required>
        <EmailAddress>
        <Display(Name:="Email")>
        Public Property Email As String
    

    After modify your Register view adding the username property

    <div class="form-group">
            @Html.LabelFor(Function(m) m.UserName, New With {.class = "col-md-2 control-label"})
            <div class="col-md-10">
                @Html.TextBoxFor(Function(m) m.UserName, New With {.class = "form-control"})
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(Function(m) m.Email, New With {.class = "col-md-2 control-label"})
            <div class="col-md-10">
                @Html.TextBoxFor(Function(m) m.Email, New With {.class = "form-control"})
            </div>
        </div>
    

    Once this is done, modify too your Login view

                    @Html.ValidationSummary(True, "", New With {.class = "text-danger"})
                <div class="form-group">
                    @Html.Label("User Name", New With {.class = "col-md-2 control-label"})
                    <div class="col-md-10">
                        @Html.TextBoxFor(Function(m) m.Email, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(m) m.Email, "", New With {.class = "text-danger"})
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(Function(m) m.Password, New With {.class = "col-md-2 control-label"})
                    <div class="col-md-10">
                        @Html.PasswordFor(Function(m) m.Password, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(m) m.Password, "", New With {.class = "text-danger"})
                    </div>
                </div>
    

    That is all you need. This way you can LogIn with the UserName not with the email address.

    0 讨论(0)
  • 2020-11-29 07:12

    I'm currently working on this feature for the Identity 1.1 templates which will switch to email and add account confirmation/forgot password functionality, and the two options we considered was the hack (use username as email with validation) and adding an additional email field which is separate from the username which is what we are leaning towards.

    Its likely that there will be a few email specific apis added in 1.1 to the UserManager:

    FindByEmail
    SetEmail
    GetEmail
    
    0 讨论(0)
  • 2020-11-29 07:13

    As you will have probably found out (and was to be expected), ASP.NET Identity 2.0.0, released March 2014, adds this functionality in the framework.

    Announcement: http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx

    Full example and tutorial, including account confirmation: http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity

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