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

ASP.NET Web Pages WebSecurity 會員系統完整參考與實作指南

     編輯:ASP.NET 基礎

ASP.NET Web Pages - WebSecurity 物件完整參考:身分驗證與會員管理核心 API


WebSecurity 物件封裝 ASP.NET SimpleMembership 提供者,整合帳戶建立、登入登出、密碼重設、角色授權等完整身分管理功能。


WebSecurity 核心屬性

屬性型別描述
CurrentUserIdInt目前登入使用者 ID (未登入回傳 -1)
CurrentUserNameString目前登入使用者名稱 (Email)
HasUserIdBoolean是否有使用者 ID (等同 IsAuthenticated)
IsAuthenticatedBoolean目前要求是否經過驗證

WebSecurity 核心方法完整參考

方法描述關鍵參數
ChangePassword(userName, currentPassword, newPassword)變更密碼 (需舊密碼驗證)使用者名、舊密碼、新密碼
ConfirmAccount(accountConfirmationToken)確認帳戶 (Email 驗證流程)驗證 Token
CreateAccount(userName, password, requireConfirmation)建立帳戶 (舊版 API)使用者名、密碼、需確認
CreateUserAndAccount(userName, password, propertyValues, requireConfirmation)建立使用者與擴充屬性 (推薦)使用者名、密碼、匿名型別屬性、需確認
GeneratePasswordResetToken(userName, tokenExpirationInMinutes)產生密碼重設 Token (寄送 Email 連結)使用者名、Token 有效分鐘數
GetCreateDate(userName)取得帳戶建立日期使用者名
GetPasswordChangeDate(userName)取得最後變更密碼日期使用者名
GetUserId(userName)依使用者名取得 UserId使用者名
InitializeDatabaseConnection(connStringName, userTable, userIdColumn, userNameColumn, autoCreateTables)初始化資料庫連線 (必要步驟)連線名、使用者表、ID 欄、名稱欄、自動建表
IsConfirmed(userName)檢查帳戶是否已確認使用者名
IsCurrentUser(userName)檢查是否為目前登入使用者使用者名
Login(userName, password, persistCookie)登入使用者 (建立驗證 Cookie)使用者名、密碼、記住我
Logout()登出 (清除驗證 Cookie)無
RequireAuthenticatedUser()未驗證則拋 401 (授權攔截)無
RequireRoles(roles...)非指定角色則拋 401角色名稱陣列
RequireUser(userName)非指定使用者則拋 401使用者名
ResetPassword(passwordResetToken, newPassword)重設密碼 (Token 驗證後)重設 Token、新密碼
UserExists(userName)檢查使用者是否存在使用者名

技術規格

項目值
類別完整名稱WebMatrix.WebData.WebSecurity
命名空間WebMatrix.WebData
組件WebMatrix.WebData.dll

資料庫初始化:_AppStart.cshtml 關鍵設定

@{
 WebSecurity.InitializeDatabaseConnection(
 connectionStringName: "DefaultConnection", // Web.config 連線字串名稱
 userTableName: "UserProfile",              // 使用者資料表
 userIdColumn: "UserId",                    // 主鍵欄位
 userNameColumn: "Email",                   // 使用者名稱欄位 (Email)
 autoCreateTables: true                     // 自動建立資料表
 );
}

參數詳細說明:

  • connectionStringName:對應 Web.config <connectionStrings> 的 name 屬性
  • userTableName:儲存使用者基本資料的表格 (預設 UserProfile)
  • userIdColumn:主鍵欄位,通常為 UserId (INT IDENTITY)
  • userNameColumn:唯一識別使用者的欄位,現代應用建議使用 Email
  • autoCreateTables:true 自動建立 UserProfile 與 webpages_Membership 表,**但不會自動建立資料庫本身** (資料庫必須預先存在)

資料庫結構:SimpleMembership 雙表架構

UserProfile 表 - 使用者基本資料

UserId (PK)Email
1john@johnson.net
2peter@peterson.com
3lars@larson.eut

webpages_Membership 表 - 會員驗證資料

UserIdCreateDateConfirmationTokenIsConfirmedPasswordPasswordChangeDate
12024-01-15NULLTrueAHashedValue...2024-01-15

其他欄位:LastPasswordFailureDate、PasswordFailuresSinceLastSuccess、PasswordVerificationToken、PasswordVerificationTokenExpirationDate 等安全稽核欄位。


常見問題:SimpleMembership 相容性設定

若部署環境與開發環境設定不一致,可能出現初始化錯誤。解決方案:在 Web.config 加入:

<appSettings>
 <add key="enableSimpleMembership" value="true" />
 <add key="autoFormsAuthentication" value="true" />
</appSettings>

遷移指引:現代專案請使用 ASP.NET Core Identity

特性WebSecurity (SimpleMembership)ASP.NET Core Identity
架構WebMatrix 封裝模組化、DI 原生、跨平台
資料存取ADO.NET 直連EF Core / 自訂 Store
密碼雜湊PBKDF2 (固定)PBKDF2/Argon2/BCrypt 可換
Two-Factor Auth不支援內建 TOTP、SMS、Email
外部登入OAuthWebSecurity (舊)Microsoft.AspNetCore.Authentication 完整支援
Claims-based不支援核心架構

現有專案可漸進式遷移:新功能採用 Identity,舊模組維持 WebSecurity 並共用 UserProfile 資料表。


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