Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




QuestPond’s Interview Questions & Answers on ASP.NET MVC (Model View Controller)

What are the steps to enable Forms Authentication in ASP.NET MVC (Model View Controller)?

1. Create a Controller called LoginController, an Action Method called Login follows.

public class LoginController:Controller

{

            publicActionResult Login()

            {

                        return view();

             }

}


2. Right click the Login action and create a view called Login which contain,

      a. Input controls for accepting user credentials
      b. A submit button
      c. Both of the above should be encapsulated inside a form tag with method set as “POST” 
          and action set to one of the action method.

<form method=”post” action=”~/Login/LoginCheck”>

            <!—add input controls and a submit button -->

</form>


3. Open web.config file located in the global level not inside views folder.

4. In system.web section add a new tag called authentication and set mode to “Forms”.

<system.web>

            <authentication mode="Forms">

            …

            </authentication>

</system.web>


5. Inside authentication add a section called Forms and set LoginUrl to action method created in step 1.

            <authentication mode="Forms">

            <forms loginUrl=”~/Login/Login” ></forms>

            </authentication>


6. Add Authorize attribute at action/controller/global level make sure that requests are authorized.

[Authorize]

publicActionResultGetCustomer()

{
}

.

.

[Authorize]

public class MyController:controller

{
}

.

.

.

public class FilterConfig

    {

public static void RegisterGlobalFilters(GlobalFilterCollection filters)

        {

filters.Add(new AuthorizeAttribute());

        }

    }


Note: In case authorize attribute is added at global level make sure attach AllowAnonymous attribute to Login and Logic actions in LoginController otherwise without login accessing them will not be possible and hence leads to dead lock and error.

7. Create a cookie inside LoginCheck action as follows and redirect to appropriate action.

publicActionResultLoginCheck()

{

            String UserName=Request.Form[“UserName”];

            String Password=Request.Form[“Password”];

            //Check username and Password

            FormsAuthentication.SetAuthCookie(UserName,false);

            returnRedirectToAction(“Home”,”MainController”);

}


For technical trainings on various topics like WCF (Windows Communication Foundation), MVC (Model View Controller), Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com
 
 For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla
 
 See the following video on Forms Authentication in ASP.NET MVC (Model View Controller): -

 
 
Click and see here for more learning ASP.NET MVC (Model View Controller)
  



Helpful Website Url
http://www.questpond.com/
Share this article   |    Print    |    Article read by 4274 times
Author:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles:
Related Interview Questions: No related interview question