Examples of Asp. Mvc 2.0 user logon and logout (2) and asp. mvc instances

Source: Internet
Author: User

Examples of Asp. Mvc 2.0 user logon and logout (2) and asp. mvc instances

This section describes ASP. MVC 2.0's user logon and logout functions. The user logon methods described in this series are FORM verification methods. Before giving a lecture, I would like to explain the <%: %> function, which is the same as the <%: %> function for dynamic output of content.
1. log on
1. Create a MODEL
During login, we generally only need to verify the user name and password, and whether to save the login COOKIE. Therefore, we need to create a MODEL login class, which only includes three fields.

/// <Summary> /// User Logon MODEL /// </summary> public class Login {// <summary> /// user name /// </summary> [DisplayName ("username")] public string UserName {get; set ;}/// <summary> /// password /// </summary> [DisplayName ("password")] public string UserPwd {get; set ;}//< summary> /// whether to save the COOKIE /// </summary> [DisplayName ("remember me")] public bool RememberMe {get; set ;}

2. Create a VIEW page
Similarly, when you log on to a VIEW page and create a strong page, you prefer to create a strong page because the page is associated with the MODEL and you can directly use the MODEL on the page. In this case, select MvcLogin. Models. Login for the view data class on the page.

<% @ Page Language = "C #" Inherits = "System. Web. Mvc. ViewPage <MvcLogin. Models. Login>" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> Login </title> 

Html. CheckBoxFor is used to generate a check box button.
3. Create a controller
Similarly, we create two login methods in the controller, one for displaying the page, and the other for clicking the logon button to determine the user name and password.

Public ActionResult Login () {return View ();} [HttpPost] public ActionResult Login (Models. login model) {if (new Models. sqlHelper (). userLogin (model) {// if the user name exists, go to the homepage FormsService. signIn (model. userName, model. rememberMe); return RedirectToAction ("index");} else {// Logon Failed, go to the logon page ViewData ["msg"] = "Logon Failed "; return View (model );}}

The second Login method has the HTTPPOST Attribute before it, so it can only accept POST requests.
4. Add a user name and password in SQLHELPER

/// <Summary> /// determine whether the user has logged on successfully. /// </summary> /// <param name = "model"> </param> // <returns> </returns> public bool UserLogin (Login model) {strUserExist = string. format (strUserExist, model. userName, model. userPwd); SqlConnection con = new SqlConnection (conStr); con. open (); SqlCommand cmd = new SqlCommand (strUserExist, con); SqlDataAdapter adp = new SqlDataAdapter (cmd); DataSet ds = new DataSet (); adp . Fill (ds); con. Close (); if (ds! = Null & ds. Tables [0]. Rows. Count> 0) {return true;} return false ;}

5. Run the logon page
At this time, we enter the URL in the page, and we will switch to the login page,
The effect is as follows:

Click "Log On". After the logon succeeds, the system returns to the home page. If the logon fails, a message is displayed.
When you click log on, the POST submission method is used. The publicActionResult Login (Models. Login model) method is called.
The logon Failure page is as follows:

The logon success page is as follows:

Ii. logout
After successfully logging on to the homepage, We will generate a logout connection on the homepage.

<P style = "font-size: 15pt; color: Red;"> <% if (Request. isAuthenticated) {%> welcome <%: Page. user. identity. name %>! <%: Html. actionLink ("logout", "LoginOff") % ><%} else {%><%: Html. actionLink ("Login", "Login") % ><%}%> </p>

Here we will introduce the Html. ActionLink method,
Html. ActionLink is used to generate a link. The first parameter represents the link problem, and the second parameter represents the actionname, which can be understood as the link page.

The code above shows that the Logout link points to LoginoFF., that is, the loginoff action Method in the controller. Therefore, we add a loginoff Method to the controller. After the loginoff method is executed, it will switch to the INDEX homepage.

<Span style = "font-family: Microsoft YaHei; font-size: 16px; "> </span> /// <summary> // user logout /// </summary> /// <returns> </returns> public ActionResult LoginOff () {FormsService. signOut (); return RedirectToAction ("index ");}

The above is an example of implementing the user login and logout function in Asp. Mvc 2.0. You can practice it on your own website and hope to make some innovations and improvements on this basis.

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.