這一節講解下ASP.MVC 2.0的用戶登錄與注銷功能,先講登錄,后說注銷。我們這個系列講的用戶登錄方式都是FORM表單驗證方式。在講之前先給大家說下<%:%>的功能,<%:%>與<%=%>功能一樣,用來動態輸出內容。
一、登錄
1. 建立MODEL
登錄的時候,我們一般只要驗證用戶名和密碼,還有是否保存登錄COOKIE,所以我們建立一個MODEL登錄類,只需包括3個字段就可以。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/// < 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頁面,同樣建立一個強類型的頁面,之所以喜歡建立強類型的頁面,是因為頁面和MODEL相關聯,在頁面中直接可以使用MODEL。此時頁面的視圖數據類應選擇MvcLogin.Models.Login。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<%@ 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用來生成一個復選框按鈕
3.建立controller
同樣我們在controller中建立兩個login方法,一個用來展現頁面,一個用來點擊登錄按鈕后判斷用戶名和密碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public ActionResult Login() { return View(); } [HttpPost] public ActionResult Login(Models.Login model) { if ( new Models.SqlHelper().UserLogin(model)) { //如果用戶名存在,轉向主頁 FormsService.SignIn(model.UserName,model.RememberMe); return RedirectToAction( "index" ); } else { //登錄失敗,轉向登錄頁面 ViewData[ "msg" ] = "登錄失敗" ; return View(model); } } |
第二個Login方法前面有HTTPPOST屬性,所以只能接受POST請求
4.SQLHELPER中添加判斷用戶名和密碼的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/// <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.運行登錄頁面
此時我們在頁面中輸入URL,就會轉向登錄頁面,
效果如下:
點擊登錄,登錄成功后轉向首頁,登錄失敗返回本頁面,并顯示提示信息。
點擊登錄的時候,是POST提交方式,會調用publicActionResult Login(Models.Login model)方法。
登錄失敗頁面如下
登錄成功頁面如下
二.注銷
登錄成功后,轉向首頁,在首頁上我們會生成注銷連接。
1
2
3
4
5
6
7
8
9
10
11
12
|
<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用來生成一個鏈接,第一個參數代表鏈接的問題,第二個參數代表的是actionname,可以理解為鏈接的頁面。
由以上代碼可以看出,注銷鏈接指向LoginoFF.,也就是controller中的loginoff action方法,所以我們在controller中添加一個一個loginoff方法,執行完loginoff方法后,會轉向INDEX首頁
1
2
3
4
5
6
7
8
9
|
<span style= "font-family:Microsoft YaHei;font-size:16px;" > </span> /// <summary> /// 用戶注銷 /// </summary> /// <returns></returns> public ActionResult LoginOff() { FormsService.SignOut(); return RedirectToAction( "index" ); } |
以上就是Asp.Mvc 2.0實現用戶登錄與注銷功能實例講解,大家可以在自己的網站上進行實踐了,希望在此基礎上可以有所創新和完善。