Tamal Marick
How do you implement Forms authentication in MVC?
By Tamal Marick in .NET on Jun 27 2019
  • Tamal Marick
    Jun, 2019 27

    Forms authentication is implemented the same way as in ASP.NET. The first step is to set the authentication mode equal to Forms. The loginUrl points to a controller here rather than a page. We also need to create a controller where we will check if the user is proper or not. If the user is proper we will set the cookie value.Hide Copy Code public ActionResult Login() {if ((Request.Form["txtUserName"] == "Tamal") && (Request.Form["txtPassword"] == "Tamal@123")){FormsAuthentication.SetAuthCookie("Tamal",true);return View("About");}else{return View("Index");} } All the other actions need to be attributed with the Authorize attribute so that any unauthorized user making a call to these controllers will be redirected to the controller (in this case the controller is “Login”) which will do the authentication.Hide Copy Code [Authorize] PublicActionResult Default() { return View(); } [Authorize] publicActionResult About() { return View(); }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS