Saturday, February 04, 2012

How to create a login page using ASP.NET MVC 3 Razor

I am back to MVC project. I used MVC for web application long time ago when it was still in Microsoft Patterns & Practice development. Now it is part of .Net 3.5 and 4.0 framework and all the classes and templates are available in Visual Studio 2011.

I need to add a Login page to my project. The way I did in my ASP.Net is quite different from the way in MVC. Since MVC has been changed a lot in terms of structure and framework, I have to learn MVC again. The MVC templates created by Visual Studio 2010 do not have Login feature. Fortunately, I found this tutorial on Youtube in short time:

The tutorial example is what I want.  It seems very easy to add Login page to MVC project.

First, add restriction in web.config file with Location element:

<configuration>
   <location path="">
      <system.web>
         <authorization>
            <deny users="?"/>
         </authorization>
      </system.web>
   </location>
</configuration>

The above will disable access to all the pages in the site.  Then add authentication element for login page in web.config:

<authentication mode="Forms">
  <forms loginUrl="~/Login/Login.aspx" timeout="120" />
</authentication>

In order to ensure the authentication in other web pages, a cooky is added before redirect to other forms on server side, as an indication been authenticated (part 3):

FormsAuthendication.SetAuthCookies(key, persistent);

In the place where log out is implemented, the following code is used to clear the cookie (part 4):

FormsAuthendication.SignOut();

That's all the basics.

In addition to those steps, the tutorial explains master page in MVC, which is in the Views->Shared folder. Different master pages can be specified (part 4). One interesting thing in MVC is that cs and html are all in one source code file as cshtml extension.

The tutorial also explains some very basic concepts in MVC:
  • Router structure in Global.assax (part 1)
  • Model for a view (part 5)
  • Get and Post actions mapping to View's methods

4 comments:

Unknown said...

I am currently working on a project but I am having difficulty writing code for the application to save updates and editing of client information could some one help. please feel free to email me at cassianodan@gmail.com

Unknown said...

I am currently working on a project but I am having difficulty writing code for the application to save updates and editing of client information could some one help. please feel free to email me at cassianodan@gmail.com

Unknown said...

I am currently working on a project but I am having difficulty writing code for the application to save updates and editing of client information could some one help. please feel free to email me at cassianodan@gmail.com

Unknown said...

I am currently working on a project but I am having difficulty writing code for the application to save updates and editing of client information could some one help. please feel free to email me at cassianodan@gmail.com