UserController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(Customers customer)
{
if (ModelState.IsValid)
{
var obj = db.Customers.Where(a => a.Username.Equals(customer.Username) && a.Password.Equals(customer.Password)).FirstOrDefault();
if (obj != null)
{
Session["IC"] = obj.IC.ToString();
Session["Username"] = obj.Username.ToString();
return RedirectToAction("AfterLogin");
}
}
return View(customer);
}
public ActionResult AfterLogin()
{
if (Session["IC"] != null)
{
return View();
}
else
{
return RedirectToAction("Login");
}
}
Login.cshtml
@model LeafLife.Models.Customers
@{
ViewBag.Title = "Login";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Login</h2>
@using (Html.BeginForm("Login", "User", FormMethod.Post))
{
<fieldset>
<legend>Mvc Simple Login Application Demo</legend>
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@if (@ViewBag.Message != null)
{
<div style="border: 1px solid red">
@ViewBag.Message
</div>
}
<table>
<tr>
<td>@Html.LabelFor(a => a.Username)</td>
<td>@Html.TextBoxFor(a => a.Username)</td>
<td>@Html.ValidationMessageFor(a => a.Username)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.Password)</td>
<td>@Html.PasswordFor(a => a.Password)</td>
<td>@Html.ValidationMessageFor(a => a.Password)</td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</fieldset>
}
Customers.cs
public class Customers
{
[Key]
public int IC { get; set; }
[Display(Name = "Username")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Username required")]
public string Username { get; set; }
[Display(Name = "First Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "First name required")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Last name required")]
public string LastName { get; set; }
[Display(Name = "Email Address")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Email Address required")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Display(Name = "Date of birth")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateofBirth { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
[MinLength(6, ErrorMessage = "Minimum 6 characters required")]
public string Password { get; set; }
[Display(Name = "Confirm Password")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Confirm password and password do not match")]
public string ConfirmPassword { get; set; }
//ForeignKey
public int MembershipTypesId { get; set; }
[ForeignKey("MembershipTypesId")]
public MembershipTypes MembershipTypes { get; set; }
}
I tested it, after i inserted the required field it just stuck on the login page. The google chrome did pop out the save password notification only once after many test. I test it again in incognito mode is won't login just displaying the Login page. I tried again on Chrome and it was the same again.