I a working on an MVC 4 website. I would like users to login with an email, password confirmation instead of the default username and password. I may have got it working, I would just like some confirmation and clarification.
I have the following Models:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Required]
public string UserName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
...
}
public class RegisterModel
{
[Required]
[Display(Name = "User Name")]
public string UserName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
...
}
In the SimpleMembershipInitializerand constructor and Configuration.Seed, I have the following:
The AccountController, Register action I have the following:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.Email, model.Password, new { UserName = model.UserName });
Roles.AddUsersToRoles(new[] { model.Email }, new[] { "StandardMember" });
WebSecurity.Login(model.Email, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
This seems to work, however I am not sure if I have all this configured correctly. When I look into AccountCOntroller.ErrorCodeToString I notice lines such as:
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
{
// See http://go.microsoft.com/fwlink/?LinkID=177550 for
// a full list of status codes.
switch (createStatus)
{
case MembershipCreateStatus.DuplicateUserName:
return "An account with that email already exists. Please use a different email or contact info@moneydrainplug.com."; // My modified version
case MembershipCreateStatus.DuplicateEmail:
return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
...
Am I doing things correctly?
If I was using the standard username, password combo, when and how would the MembershipCreateStatus.DuplicateEmail be triggered?
Sorry, I am quite confused by all of this, I hope the question is clear.
Daryn
0 Points
1 Post
MVC 4 - Simplemembership - replacing username with email
Oct 23, 2012 12:14 PM|LINK
I a working on an MVC 4 website. I would like users to login with an email, password confirmation instead of the default username and password. I may have got it working, I would just like some confirmation and clarification.
I have the following Models:
[Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } [Required] public string UserName { get; set; } [Required] [EmailAddress] public string Email { get; set; } ... } public class RegisterModel { [Required] [Display(Name = "User Name")] public string UserName { get; set; } [Required] [EmailAddress] [Display(Name = "Email")] public string Email { get; set; } ... }In the SimpleMembershipInitializerand constructor and Configuration.Seed, I have the following:
The AccountController, Register action I have the following:
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { WebSecurity.CreateUserAndAccount(model.Email, model.Password, new { UserName = model.UserName }); Roles.AddUsersToRoles(new[] { model.Email }, new[] { "StandardMember" }); WebSecurity.Login(model.Email, model.Password); return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }This seems to work, however I am not sure if I have all this configured correctly. When I look into AccountCOntroller.ErrorCodeToString I notice lines such as:
private static string ErrorCodeToString(MembershipCreateStatus createStatus) { // See http://go.microsoft.com/fwlink/?LinkID=177550 for // a full list of status codes. switch (createStatus) { case MembershipCreateStatus.DuplicateUserName: return "An account with that email already exists. Please use a different email or contact info@moneydrainplug.com."; // My modified version case MembershipCreateStatus.DuplicateEmail: return "A user name for that e-mail address already exists. Please enter a different e-mail address."; ...Am I doing things correctly?
If I was using the standard username, password combo, when and how would the MembershipCreateStatus.DuplicateEmail be triggered?
Sorry, I am quite confused by all of this, I hope the question is clear.
Thanks in advance...
SimpleMembership EntityFramework
bilalashraft...
Member
497 Points
122 Posts
Re: MVC 4 - Simplemembership - replacing username with email
Dec 12, 2012 06:53 AM|LINK
Keep the default membership as is. Just write the below code in Login Post Action
if (UserName.Contains("@")) //Email Login //or check email using Regular Expression { string username = Membership.GetUserNameByEmail(UserName ); if (username != null) { if (Membership.ValidateUser(username, Password)) { //user is validated } else{ //invalid user } } } else //Standard Username & Password Login { if(Membership.ValidateUser(UserName, Password)) //user validated else // invalid user }http://www.ointerns.com
http://www.webbsa.com
http://www.mycarpoint.com