Sunday 31 May 2015

Create Custom Login with MVC 5

1) Create Model class for Login

    public class LoginModel
    {
        [Required(ErrorMessage = "Please enter Email ID")]
        public string EmailID { get; set; }

        [Required(ErrorMessage = "Please enter Password")]
        public string Password { get; set; }
    }


2) Here is code for Controller

       public ActionResult Login()
       {
            return View();
       }

        [HttpPost]
        public ActionResult Login(LoginModel objLoginModel)
        {
            if (ModelState.IsValid)
            {
                // Here you can also check Email and Password from Database
                if (objLoginModel.EmailID == objLoginModel.Password)
                {
                    FormsAuthentication.SetAuthCookie(objLoginModel.EmailID, false);
                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login", "Account");
        }



No comments:

Post a Comment