欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Asp.Mvc 2.0實現(xiàn)用戶登錄與注銷功能實例講解(2)

 更新時間:2022年04月27日 14:43:59   作者:zx13525079024  
這篇文章主要介紹了Asp.Mvc 2.0實現(xiàn)用戶登錄與注銷功能,用戶登錄方式都是FORM表單驗證方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這一節(jié)講解下ASP.MVC 2.0的用戶登錄與注銷功能,先講登錄,后說注銷。我們這個系列講的用戶登錄方式都是FORM表單驗證方式。在講之前先給大家說下<%:%>的功能,<%:%>與<%=%>功能一樣,用來動態(tài)輸出內(nèi)容。

一、登錄

1. 建立MODEL

登錄的時候,我們一般只要驗證用戶名和密碼,還有是否保存登錄COOKIE,所以我們建立一個MODEL登錄類,只需包括3個字段就可以。

/// <summary> 
 /// 用戶登錄MODEL 
 /// </summary> 
 public class Login 
 { 
  
  /// <summary> 
  /// 用戶名 
  /// </summary> 
  [DisplayName("用戶名")] 
  public string UserName 
  { 
   get; 
   set; 
  } 
 
  /// <summary> 
  /// 密碼 
  /// </summary> 
  [DisplayName("密碼")] 
  public string UserPwd 
  { 
   get; 
   set; 
  } 
 
  /// <summary> 
  /// 是否保存COOKIE 
  /// </summary> 
  [DisplayName("記住我")] 
  public bool RememberMe 
  { 
   get; 
   set; 
  } 

2.建立VIEW頁面

同樣登錄的VIEW頁面,同樣建立一個強(qiáng)類型的頁面,之所以喜歡建立強(qiáng)類型的頁面,是因為頁面和MODEL相關(guān)聯(lián),在頁面中直接可以使用MODEL。此時頁面的視圖數(shù)據(jù)類應(yīng)選擇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 xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
 <title>Login</title> 
</head> 
<body> 
<div style="font-size:15pt;color:Red;"> 
 <%if (ViewData["msg"] != null) 
 {%> 
 <%:ViewData["msg"].ToString()%> 
 <%} %> 
</div> 
 <div> 
 <%Html.BeginForm();%> 
  
 <table> 
  <tr> 
  <td></td> 
  <td>用戶登錄</td> 
  </tr> 
  
  <tr> 
  <td><%:Html.LabelFor(m=>m.UserName) %></td> 
  <td><%:Html.TextBoxFor(m=>m.UserName)%></td> 
  </tr> 
 
   <tr> 
  <td><%:Html.LabelFor(m=>m.UserPwd) %></td> 
  <td><%:Html.PasswordFor(m=>m.UserPwd) %></td> 
  </tr> 
 
   <tr> 
  <td><%:Html.LabelFor(m=>m.RememberMe) %></td> 
  <td><%:Html.CheckBoxFor(m=>m.RememberMe) %></td> 
  </tr> 
 
 <tr> 
  <td></td> 
  <td><input type="submit" value="登錄" /></td> 
  </tr> 
 </table> 
 <%Html.EndForm(); %> 
 </div> 
</body> 
</html> 

Html.CheckBoxFor用來生成一個復(fù)選框按鈕

3.建立controller

同樣我們在controller中建立兩個login方法,一個用來展現(xiàn)頁面,一個用來點擊登錄按鈕后判斷用戶名和密碼

public ActionResult Login() 
  { 
   return View(); 
  } 
 
  [HttpPost] 
  public ActionResult Login(Models.Login model) 
  { 
   if (new Models.SqlHelper().UserLogin(model)) 
   { 
    //如果用戶名存在,轉(zhuǎn)向主頁 
    FormsService.SignIn(model.UserName,model.RememberMe); 
    return RedirectToAction("index"); 
   } 
   else 
   { 
    //登錄失敗,轉(zhuǎn)向登錄頁面 
    ViewData["msg"] = "登錄失敗"; 
    return View(model); 
   } 
 
   
  } 

第二個Login方法前面有HTTPPOST屬性,所以只能接受POST請求

4.SQLHELPER中添加判斷用戶名和密碼的方法

/// <summary> 
  /// 判斷用戶登錄是否成功 
  /// </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.運(yùn)行登錄頁面

此時我們在頁面中輸入URL,就會轉(zhuǎn)向登錄頁面,

效果如下:

點擊登錄,登錄成功后轉(zhuǎn)向首頁,登錄失敗返回本頁面,并顯示提示信息。
點擊登錄的時候,是POST提交方式,會調(diào)用publicActionResult Login(Models.Login model)方法。
登錄失敗頁面如下

登錄成功頁面如下

二.注銷

登錄成功后,轉(zhuǎn)向首頁,在首頁上我們會生成注銷連接。

<p style="font-size:15pt; color:Red;"> 
  <%if (Request.IsAuthenticated) 
   {%> 
   歡迎您<%:Page.User.Identity.Name%>! 
   
   <%:Html.ActionLink("注銷", "LoginOff")%> 
   <%} 
   else 
   {%> 
   <%:Html.ActionLink("登錄", "Login")%> 
   <%} %> 
</p> 

這里介紹下Html.ActionLink方法,Html.ActionLink用來生成一個鏈接,第一個參數(shù)代表鏈接的問題,第二個參數(shù)代表的是actionname,可以理解為鏈接的頁面。

由以上代碼可以看出,注銷鏈接指向LoginoFF.,也就是controller中的loginoff action方法,所以我們在controller中添加一個一個loginoff方法,執(zhí)行完loginoff方法后,會轉(zhuǎn)向INDEX首頁

/// <summary> 
  /// 用戶注銷 
  /// </summary> 
  /// <returns></returns> 
  public ActionResult LoginOff() 
  { 
   FormsService.SignOut(); 
   return RedirectToAction("index"); 
  } 

以上就是Asp.Mvc 2.0實現(xiàn)用戶登錄與注銷功能實例講解,大家可以在自己的網(wǎng)站上進(jìn)行實踐了,希望在此基礎(chǔ)上可以有所創(chuàng)新和完善。

相關(guān)文章

最新評論