This article has expired. Learning only
Development Platform:
VS2008 RTM
SQL SERVER 2005 Enterprise Edition
ASP. NET 3.5 Extensions CTP
MVCToolkit
1. Create an MVC project. Just name it.
2. Create a SQL database named mvc_test
3. Run C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_regsql.exe to generate the Membership structure of the database.
Click Finish next.
4. Now the Membership structure has been generated.
5. Add the SQL connection string to Web. Config.
<ConnectionStrings>
<Remove name = "LocalSqlServer"/>
<Add name = "LocalSqlServer" connectionString = "Data Source = .; initial Catalog = mvc_test; Persist Security Info = True; User ID = sa; Password = "providerName =" System. data. sqlClient "/>
</connectionStrings>
Do not modify the configuration name = "LocalSqlServer"
6. Add the membership configuration node to Web. Config)
<Membership defaultProvider = "SQLMembershipProvider">
<Providers>
<Add name = "SQLMembershipProvider" passwordFormat = "Clear" type = "System. web. security. sqlMembershipProvider "connectionStringName =" LocalSqlServer "applicationName =" mvc_test "Success =" true "enablePasswordReset =" true "Success =" false "requiresUniqueEmail =" false "Success =" 4 "Success =" 0 "/>
</Providers>
</membership>
7. Change <authentication mode = "Windows"/> to <authentication mode = "Forms"/>
8. Create an MVC View Content Page in the Views/Home directory and name it Login. aspx.
Next, select the Views/Shared/Site. Master page.
8. Put the Login. aspx page into a User Control. Therefore, create an MVC View User Control.
Name it Login. ascx, which is created under the Views/Shared directory.
9. Replace the following code with the automatically generated one:
<% @ Control Language = "C #" Inherits = "System. Web. Mvc. ViewUserControl" %>
<% If (! Page. User. Identity. IsAuthenticated)
{%>
<Form action = '<% = Url. Action (new {Action = "Login", Controller = "Home"}) %>' method = "post">
Username: <% = Html. TextBox ("Username") %>
Password: <% = Html. Password ("Password") %> <% = ViewData. ContainsDataItem ("Message ")? ViewData ["Message"]: "" %> <% = Html. SubmitButton ("Submit", "OK") %>
</Form>
<%}
Else
{%>
<Span> hello!
<% = Page. User. Identity. Name %> <% = Html. ActionLink <MVC_Login.Controllers.HomeController> (c => c. Logout ()
, "Log out", new {id = "LogoutLink"}) %> </span>
<%} %>
10. The method for referencing User Control is
<% = Html. RenderUserControl ("~ /Views/Shared/Login. ascx ") %>
Add this code to the appropriate location of Login. aspx and master pages.
11. Open the Controllers/HomeController. cs file.
Add the following two controller actions
[ControllerAction]
Public void Login ()
{
If (! ControllerContext. HttpContext. User. Identity. IsAuthenticated)
{
If (Request. RequestType = "POST ")
{
String userName = Request. Form ["Username"];
String password = Request. Form ["Password"];
If (Membership. ValidateUser (userName, password ))
{
FormsAuthentication. SetAuthCookie (userName, true );
// Set cookie and redirect
RedirectToAction ("Index ");
}
Else
{
ViewData. Add ("Message", "incorrect user name or password ");
}
}
}
RenderView ("Login ");
}
[ControllerAction]
Public void Logout ()
{
FormsAuthentication. SignOut ();
RedirectToAction ("Index ");
}
12. Download MVCToolkit. dll from the Internet. Without this, it will never work ~
: Http://asp.net/downloads/3.5-extensions/MVCToolkit.zip
Reference MVCToolkit. dll in it. Now you can run it.
By the way, you should first create a Membership user for testing. MVC registration has not yet been done