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:
- How to create a login page using ASP.NET MVC 3 Razor (5 part serials)
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):
In the place where log out is implemented, the following code is used to clear the cookie (part 4):
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