ASP.MVC 2.0 implementation of user login and Cancellation function example Explanation (2) _ Practical skills

Source: Internet
Author: User
Tags function examples actionlink

This section of the next ASP.MVC 2.0 user login and logoff function, first speak login, after said Logout. Our series of user login methods are form Forms authentication mode. Before you talk about the <%:%> function,<%:%> and <%=%> function, used to dynamically output content.
First, login
1. Establish model
When you log in, we usually just verify the username and password, and whether to save the login cookie, so we set up a model login class, just include 3 fields.

<summary> 
 ///User Login model 
 ///</summary> public 
 class login 
 { 
  
  ///<summary> 
  ///username 
  ///</summary> 
  [DisplayName ("User name")] public 
  string UserName 
  {get 
   ; 
   Set; 
  } 
 
  <summary> 
  ///password 
  ///</summary> 
  [DisplayName ("password")] public 
  string Userpwd 
  {Get 
   ; 
   Set; 
  } 
 
  <summary> 
  ///whether to save cookies 
  ///</summary> 
  [DisplayName ("Remember Me")] public 
  bool RememberMe 
  {get 
   ; 
   Set 
  

2. Create a View page
The same login View page, also set up a strong type of page, the reason is like to build strong type of page, because the page and model associated, in the page can directly use model. The View data class for the page should select MvcLogin.Models.Login.

<%@ 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.checkboxfor used to generate a check box button
3. Establishment of Controller
Similarly, we set up two login methods in controller, one for displaying the page and one for clicking on the login button to determine the username and password.

Public ActionResult Login () 
  {return 
   View (); 
  } 
 
  [HttpPost] 
  Public ActionResult Login (Models.login model) 
  { 
   if (new Models.sqlhelper (). Userlogin (model)) 
   { 
    //If username exists, turn to homepage 
    formsservice.signin (model. Username,model. RememberMe); 
    Return redirecttoaction ("index"); 
   } 
   else 
   { 
    //Login failed, turn to login page 
    viewdata["msg"] = "Login Failed"; 
    return View (model); 
   } 
 
   
   

The second login method is preceded by the HttpPost property, so only post requests can be accepted
4.SQLHELPER to add a way to judge a user name and password

<summary> 
  ///To determine whether the user login 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 login page
At this point we enter the URL in the page, we will turn to the login page,
The effect is as follows:

Click to login, login successfully turned to the home page, Login failed to return to this site, and display a message.
Click the login, is the post submission method, will call Publicactionresult login (Models.login model) method.
The login failure page is as follows

The Login Success page is as follows

Two. Cancellation
After the successful login, turn to the home page, on the home page we will generate logout connection.

 <p style= "FONT-SIZE:15PT; color:red; " > 
  <%if (request.isauthenticated) 
   {%> 
   welcome you <%:P age.user.identity.name%>! 
   
   <%:html.actionlink ("Logoff", "Loginoff")%> 
   <%} 
   else 
   {%> 
   <%:html.actionlink (" Login ", login")%> 
   <%}%> 
  </p> 

This article introduces the next Html.ActionLink method,
Html.ActionLink is used to generate a link, the first parameter represents the problem of the link, and the second parameter represents the ActionName, which can be understood as a linked page.

As you can see from the above code, the logout link points to Loginoff, which is the Loginoff action method in controller, so we add a Loginoff method to the controller, after the Loginoff method is executed, will turn to index home

<span style= "Font-family:microsoft yahei;font-size:16px;" > </span>///<summary> 
  ///user logoff 
  ///</summary> 
  ///<returns></returns> Public 
  ActionResult Loginoff () 
  { 
   formsservice.signout (); 
   Return redirecttoaction ("index"); 
  } 

The above is ASP.MVC 2.0 implementation of user login and cancellation function examples to explain, we can practice on their own website, hope that on this basis can be innovative and perfect.

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.