1

I'm starting a brand new ASP.NET MVC application using the built in "Internet application" template.

How can I set it up to allow login using email address & password instead of username & password?

JustinStolle
  • 4,182
  • 3
  • 37
  • 48
Matt Frear
  • 52,283
  • 12
  • 78
  • 86

3 Answers3

1

If you're using the default AccountModels you might as well change the LogOnModel to remove UserName and add Email, like this:

public class LogOnModel {
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

Change occurrences of UserName to Email in your LogOn view as well.

Finally in your AccountController, change the LogOn action to get the UserName from the email address to complete the SignIn process, like this:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl) {
  if (ModelState.IsValid) {
    var userName = Membership.GetUserNameByEmail(model.Email);
    if (MembershipService.ValidateUser(userName, model.Password)) {
      FormsService.SignIn(userName, model.RememberMe);
      if (Url.IsLocalUrl(returnUrl)) {
        return Redirect(returnUrl);
      } else {
        return RedirectToAction("Index", "Home");
      }
    } else {
      ModelState.AddModelError("", "The user name or password provided is incorrect.");
    }
  }

  // If we got this far, something failed, redisplay form
  return View(model);
}
JustinStolle
  • 4,182
  • 3
  • 37
  • 48
1

we have same requirement and do this, you should create custom identity (CustomIdentity : IIdentity, ISerializable)

[Serializable]
public class CustomIdentity : IIdentity, ISerializable

and define Name property for it.

then for get method of name property return email of user

    public string Name 
    { 
        get{ return Email; }
    }

if detail be needed, please comment on this post

Reza F.Rad
  • 230
  • 3
  • 13
0

Isn't the solution as simple as using the name field as an email address?

John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • Yeah I did think of that, but I want to keep using the User name property to display their name in the site, e.g. "Welcome User Name", not "Welcome user@somewhere.com". – Matt Frear Jan 14 '11 at 17:12
  • 1
    @Matt Frear: you could use a profile property to store the user's preferred name for this purpose. See http://msdn.microsoft.com/en-us/library/d8b58y5d.aspx – JustinStolle Apr 06 '11 at 16:37