Wednesday, December 30, 2015

Started Personal Git Repository

I just pushed whatever i am learning for MVC5 on my personal Git repository-https://github.com/Nirav-Bhadradiya/learn.git
Below is the repository - anyone can point that and look at the stuff. I will be pushing more in next coming days.
Helpful for beginners.!!!! 

Setup Form Authentication in MVC 5

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.