首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

ASP.NET MVC 2.0 使用者登入與登出功能實作(第 2 節)

     編輯:ASP.NET 基礎

前言

本節實作 ASP.NET MVC 2.0 表單驗證登入/登出功能,涵蓋 Model 設計、強型別 View、Controller 雙 Action 模式、FormsAuthentication 票據管理、登出導向首頁。

語法提示:<%: %> vs <%= %>

<%: %> 自動 HTML 編碼輸出(防 XSS),功能等同 <%= %> 但更安全,MVC 2 引入,建議優先使用。

一、登入 Model

public class Login {
    [DisplayName("使用者名稱")]
    public string UserName { get; set; }

    [DisplayName("密碼")]
    public string UserPwd { get; set; }

    [DisplayName("記住我")]
    public bool RememberMe { get; set; }
}

二、登入 View (強型別)

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>


登入

    
<% if (ViewData["msg"] != null) { %><%: ViewData["msg"] %><% } %>
<% Html.BeginForm(); %>
使用者登入
<%: Html.LabelFor(m => m.UserName) %> <%: Html.TextBoxFor(m => m.UserName) %>
<%: Html.LabelFor(m => m.UserPwd) %> <%: Html.PasswordFor(m => m.UserPwd) %>
<%: Html.LabelFor(m => m.RememberMe) %> <%: Html.CheckBoxFor(m => m.RememberMe) %>
<% Html.EndForm(); %>

三、Controller 雙 Action 模式

public class UserController : Controller {
    // GET: 顯示登入頁面
    public ActionResult Login() {
        return View();
    }

    // POST: 處理登入提交
    [HttpPost]
    public ActionResult Login(Models.Login model) {
        if (new Models.SqlHelper().UserLogin(model)) {
            // 登入成功:建立 FormsAuthentication Ticket
            FormsService.SignIn(model.UserName, model.RememberMe);
            return RedirectToAction("Index");
        }
        
        // 登入失敗
        ViewData["msg"] = "登入失敗,帳號或密碼錯誤";
        return View(model);  // 保留輸入值回傳 View
    }
}

四、SqlHelper 驗證邏輯

public bool UserLogin(Login model) {
    string sql = "SELECT * FROM UserInfo WHERE UserName=@u AND UserPwd=@p";
    using (SqlConnection cn = new SqlConnection(conStr)) {
        cn.Open();
        using (SqlCommand cmd = new SqlCommand(sql, cn)) {
            cmd.Parameters.AddWithValue("@u", model.UserName);
            cmd.Parameters.AddWithValue("@p", model.UserPwd);  // 實務應比對雜湊
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0].Rows.Count > 0;
        }
    }
}

五、執行效果

登入頁面 登入失敗提示 登入成功首頁

六、登出功能實作

首頁顯示登入狀態與登出連結

<% if (Request.IsAuthenticated) { %> 歡迎您 <%: Page.User.Identity.Name %>! <%: Html.ActionLink("登出", "LoginOff") %> <% } else { %> <%: Html.ActionLink("登入", "Login") %> <% } %>

Controller 登出 Action

public ActionResult LoginOff() {
    FormsService.SignOut();  // 清除 FormsAuthentication Cookie
    return RedirectToAction("Index");
}

FormsService 封裝類別

public static class FormsService {
    public static void SignIn(string userName, bool rememberMe) {
        FormsAuthentication.SetAuthCookie(userName, rememberMe);
    }
    
    public static void SignOut() {
        FormsAuthentication.SignOut();
    }
}

Html.ActionLink 參數說明

<%: Html.ActionLink("連結文字", "ActionName", "ControllerName", routeValues, htmlAttributes) %>

<%: Html.ActionLink("登出", "LoginOff", "User") %>

表單驗證 Web.config 設定


    
        
    
    
          
    

結語

MVC 2.0 表單驗證登入/登出流程清晰:Model 定義資料結構,View 強型別繫結,Controller 雙 Action 分離關注點,FormsAuthentication 管理身分票據。這套模式延續至今仍是 ASP.NET Core Identity 的基礎架構雛形。實務專案請務必:密碼雜湊儲存、HTTPS 傳輸、防 CSRF (AntiForgeryToken)、實作記住我永久 Cookie 機制。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved