Implementation of Membership login in Microsoft ASP. NET MVC

Source: Internet
Author: User
Tags set cookie

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.