---
title: "How to Implement form Authentication in MVC?"  
description: "How to Implement form Authentication in MVC?"  
author: "Manish Kumar"  
published: 2017-06-17  
updated: 2020-09-23  
canonical: https://www.mindstick.com/interview/23265/how-to-implement-form-authentication-in-mvc  
category: "c#"  
tags: ["mvc4"]  
reading_time: 2 minutes  

---

# How to Implement form Authentication in MVC?

Step 1- Set the form authentication mode equals to form

Step 2-In web.config add following code

Inside system.web

```
    <authentication mode="Forms">      <forms loginUrl="~/Home/SignIn" timeout="2880"/>    </authentication>
```

Step 3-In this step we need to controller for checking authentication. Here we check that user is authorize or not.

In home controller add following code

```
        [Authorize]       
public ActionResult About()       
{            return View();//This
view is viewed by authenticated user only       
}        [HttpPost]       
public ActionResult SignIn(Login Model)       
{             if
((Request.Form["txtUserName"] == "Manish")
&&        
(Request.Form["txtPassword"] == "Mindstick"))            {                FormsAuthentication.SetAuthCookie("Manish", true);                return View("About");            }            else            {                return View("Index");            }
```

\

Action decorated with Authorize cannot be called by unauthorized user.

## Answers

### Answer by Manish Kumar

Step 1- Set the form authentication mode equals to form

Step 2-In web.config add following code

Inside system.web

```
    <authentication mode="Forms">      <forms loginUrl="~/Home/SignIn" timeout="2880"/>    </authentication>
```

Step 3-In this step we need to controller for checking authentication. Here we check that user is authorize or not.

In home controller add following code

```
        [Authorize]       
public ActionResult About()       
{            return View();//This
view is viewed by authenticated user only       
}        [HttpPost]       
public ActionResult SignIn(Login Model)       
{             if
((Request.Form["txtUserName"] == "Manish")
&&        
(Request.Form["txtPassword"] == "Mindstick"))            {                FormsAuthentication.SetAuthCookie("Manish", true);                return View("About");            }            else            {                return View("Index");            }
```

\

Action decorated with Authorize cannot be called by unauthorized user.


---

Original Source: https://www.mindstick.com/interview/23265/how-to-implement-form-authentication-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
