I was just trying Form authentication in MVC5 and i faced some problems while setting it up. i want to share it with you guys so you will not spend your precious time to find out solution.
Step 1: Add below tag in your project's web.config. Remember that you will see one web.config in View folder as well but that is for only View purpose.
<!-- attribute loginUrl is case sensitive please don't change it -->
<authentication mode="Forms">
<forms loginUrl="~/Authentication/Login"></forms>
</authentication>
Here Login URL will be the View where you wanted to redirect your users when they tried accessing unauthorized Action methods.
Step 2; Add below attribute on your methods which you wanted to make secure. in this case i tried with my main action method of one of my Employee.cs class. You can make your whole controller secured with putting Authorize attribute for it.
[Authorize]
public ActionResult Index()
{
var employeeListViewModel = new EmployeeListViewModel {UserName = User.Identity.Name};
.................................................................
............................................................................
}
Step 3: Now Modify your Login() Action to set cookies of loggedin user FormsAuthentication.SetAuthCookie(u.UserName, false);
Step 4: Browse your Action method and it will take you to Login View.