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?
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?
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);
}
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
Isn't the solution as simple as using the name field as an email address?