登入驗證機制實作

登入驗證機制實作

在 ASP.NET 2.0 提供的 Membership 功能,可以很方便的製作登入驗證的動作;不過有時因為特殊需求需要自行製作登入驗證機制,例如製作現行構系統的 Web 前端,可能就需要呼叫中間層去執行登入驗證的動作,在此提供一個登入驗證機制的實作。

我們將登入驗證機制包含在一個獨立組件中,新的網站應用程式只需參考這個組件就可以使用登入驗證機制。此組件中包的類別如下表及下圖所示。

型別說明
TUserInfo使用者資訊。
TBBasePage頁面基礎類別。
TBLoginPage登入頁面基礎類別。

 

image

 

.TUserInfo 類別

其中 IUserInfo 介面是定義在更上層的組件中,是為了三層式架構中傳遞使用者資訊使用,例如 Web 組件(ASP.NET)傳遞到中間層組件(.NET)使用,當然在這個案例中,你也可以直接定義 TUserInfo 類別即可。

TUserInfo 類別的程式碼如下,此案例中只會使用到 IsLogin 屬性。

 

   1:  Public Class TUserInfo
   2:      Implements IUserInfo
   3:   
   4:      Private FID As String = String.Empty
   5:      Private FName As String = String.Empty
   6:      Private FGuid As Guid = Guid.Empty
   7:      Private FHandle As Integer = 0
   8:      Private FSessionID As String = String.Empty
   9:      Private FIsLogin As Boolean = False
  10:   
  11:      ''' <summary>
  12:      ''' 建構函式。
  13:      ''' </summary>
  14:      Sub New()
  15:          FGuid = Guid.NewGuid()
  16:      End Sub
  17:   
  18:      ''' <summary>
  19:      ''' 使用者帳號。
  20:      ''' </summary>
  21:      Public Property ID() As String Implements IUserInfo.ID
  22:          Get
  23:              Return FID
  24:          End Get
  25:          Set(ByVal value As String)
  26:              FID = value
  27:          End Set
  28:      End Property
  29:   
  30:      ''' <summary>
  31:      ''' 使用者名稱。
  32:      ''' </summary>
  33:      Public Property Name() As String Implements IUserInfo.Name
  34:          Get
  35:              Return FName
  36:          End Get
  37:          Set(ByVal value As String)
  38:              FName = value
  39:          End Set
  40:      End Property
  41:   
  42:      ''' <summary>
  43:      ''' 執行階段使用者識別碼。
  44:      ''' </summary>
  45:      Public Property Guid() As System.Guid Implements IUserInfo.Guid
  46:          Get
  47:              Return FGuid
  48:          End Get
  49:          Set(ByVal value As System.Guid)
  50:              FGuid = value
  51:          End Set
  52:      End Property
  53:   
  54:      ''' <summary>
  55:      ''' 執行階段使用者識別控制碼。
  56:      ''' </summary>
  57:      Public Property Handle() As Integer Implements IUserInfo.Handle
  58:          Get
  59:              Return FHandle
  60:          End Get
  61:          Set(ByVal value As Integer)
  62:              FHandle = value
  63:          End Set
  64:      End Property
  65:   
  66:      ''' <summary>
  67:      ''' 執行階段使用者識別字串。
  68:      ''' </summary>
  69:      Public Property SessionID() As String Implements IUserInfo.SessionID
  70:          Get
  71:              Return FSessionID
  72:          End Get
  73:          Set(ByVal value As String)
  74:              FSessionID = value
  75:          End Set
  76:      End Property
  77:   
  78:      ''' <summary>
  79:      ''' 是否已登入。
  80:      ''' </summary>
  81:      Public Property IsLogin() As Boolean Implements IUserInfo.IsLogin
  82:          Get
  83:              Return FIsLogin
  84:          End Get
  85:          Set(ByVal value As Boolean)
  86:              FIsLogin = value
  87:          End Set
  88:      End Property
  89:  End Class

 

.TBBasePage 類別

TBBasePage 是頁面基礎類別,主要做判斷是否登入,若未登入則轉址到定義的登入頁面。TBBasePage 類別的主要屬性如下表所示。

屬性說明
IsCheckLogin是否做登入驗證,預設為 True。
繼承 TBBasePage 的頁面預設是需要做登入驗證的,若有頁面不需做登入驗證(如登入頁面),則需要覆寫此屬性傳回 False。
LoginUrl登入頁面網址。
預設是讀取 web.config 中 appSettings 區段的 LoginUrl 參數值。你也可以覆寫此屬性,改寫登入頁面網址的定義方式。
UserInfo使用者資訊。此屬性是儲存於 Seesion 中。

 

TBBasePage 類別的完整程式碼如下所示,其中覆寫 OnInit 方法,判斷此頁面是否做登入驗證;若需要做登入驗證時,則使用 UserInfo.IsLogin 來判斷是否已登入,若未登入則轉址到 LoginUrl 屬性指定的登入頁面網址。

   1:  Imports Bee.App
   2:   
   3:  ''' <summary>
   4:  ''' 頁面基礎類別。
   5:  ''' </summary>
   6:  Public Class TBBasePage
   7:      Inherits System.Web.UI.Page
   8:   
   9:      Private Const KEY_USERINFO As String = "_UserInfo"
  10:      Private FUserInfo As TUserInfo = Nothing
  11:   
  12:   
  13:      ''' <summary>
  14:      ''' 是否做登入驗證。
  15:      ''' </summary>
  16:      Protected Overridable ReadOnly Property IsCheckLogin() As Boolean
  17:          Get
  18:              Return True
  19:          End Get
  20:      End Property
  21:   
  22:      ''' <summary>
  23:      ''' 取得 Web.config 中 appSettings 區段定義的參數。
  24:      ''' </summary>
  25:      Public Overloads Function AppSettings(ByVal Name As String) As String
  26:          Return AppSettings(Name, String.Empty)
  27:      End Function
  28:   
  29:      ''' <summary>
  30:      ''' 取得 Web.config 中 appSettings 區段定義的參數。
  31:      ''' </summary>
  32:      Public Overloads Function AppSettings(ByVal Name As String, ByVal DefaultValue As String) As String
  33:          Dim sValue As String
  34:   
  35:          sValue = System.Web.Configuration.WebConfigurationManager.AppSettings(Name)
  36:          If sValue Is Nothing Then
  37:              Return DefaultValue
  38:          Else
  39:              Return sValue
  40:          End If
  41:      End Function
  42:   
  43:      ''' <summary>
  44:      ''' 登入頁面網址。
  45:      ''' </summary>
  46:      Public Overridable ReadOnly Property LoginUrl() As String
  47:          Get
  48:              Dim sUrl As String
  49:              sUrl = Me.AppSettings("LoginUrl")
  50:              If String.IsNullOrEmpty(sUrl) Then
  51:                  Throw New Exception("未設定登入頁面")
  52:              Else
  53:                  Return sUrl
  54:              End If
  55:          End Get
  56:      End Property
  57:   
  58:      ''' <summary>
  59:      ''' 使用者資訊。
  60:      ''' </summary>
  61:      Public ReadOnly Property UserInfo() As TUserInfo
  62:          Get
  63:              If FUserInfo Is Nothing Then
  64:                  If Me.Session(KEY_USERINFO) Is Nothing Then
  65:                      FUserInfo = New TUserInfo()
  66:                      Me.Session(KEY_USERINFO) = FUserInfo
  67:                  Else
  68:                      FUserInfo = CType(Me.Session(KEY_USERINFO), TUserInfo)
  69:                  End If
  70:              End If
  71:              Return FUserInfo
  72:          End Get
  73:      End Property
  74:   
  75:      Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
  76:          '判斷是否做登入驗證
  77:          If Me.IsCheckLogin Then
  78:              '判斷是否登入,若未登入則轉址到登入頁面
  79:              If Not Me.UserInfo.IsLogin Then
  80:                  Page.Response.Redirect(Me.LoginUrl)
  81:              End If
  82:          End If
  83:   
  84:          MyBase.OnInit(e)
  85:      End Sub
  86:  End Class

 

 

.TBLoginPage 類別

TBLoginPage 類別是登入頁面基礎類別。TBLoginPage 類別是繼承 TBBasePage 類別,因為它是登入頁面所需無須做登入驗證,所以覆寫 IsCheckLogin 方法傳回 False。自行撰寫的登入頁面可以繼承 TBLoginPage 類別,並覆寫 ExecuteLogin 方法實作登入驗證的動作;按下登入鈕時,則是呼叫 Login 方法來進行登入。

   1:  ''' <summary>
   2:  ''' 登入頁面基礎類別。
   3:  ''' </summary>
   4:  Public MustInherit Class TBLoginPage
   5:      Inherits TBBasePage
   6:   
   7:      ''' <summary>
   8:      ''' 是否做登入驗證。
   9:      ''' </summary>
  10:      Protected Overrides ReadOnly Property IsCheckLogin() As Boolean
  11:          Get
  12:              Return False
  13:          End Get
  14:      End Property
  15:   
  16:      ''' <summary>
  17:      ''' 執行登入驗證,衍生類別必須覆寫。
  18:      ''' </summary>
  19:      ''' <param name="UserID">登入帳號。</param>
  20:      ''' <param name="Password">登入密碼。</param>
  21:      ''' <returns>登入成功傳回 Treu,反之傳回 False。</returns>
  22:      Protected MustOverride Function ExecuteLogin(ByVal UserID As String, ByVal Password As String) As Boolean
  23:   
  24:      ''' <summary>
  25:      ''' 執行登入。
  26:      ''' </summary>
  27:      ''' <param name="UserID">登入帳號。</param>
  28:      ''' <param name="Password">登入密碼。</param>
  29:      Protected Overridable Sub Login(ByVal UserID As String, ByVal Password As String)
  30:          If ExecuteLogin(UserID, Password) Then
  31:              Me.UserInfo.IsLogin = True
  32:              Me.Response.Redirect("~/")
  33:          Else
  34:              Me.ClientScript.RegisterStartupScript(Me.GetType, "LoginError", "alert('登入錯誤');", True)
  35:          End If
  36:      End Sub
  37:   
  38:  End Class

 

 

.撰寫 ASP.NET 網站應用程式測試登入

再來新增一個新的 ASP.NET 網站應用程式,首先在其 web.config 中定義登入頁面網址為 "~/Login.aspx"。

  <appSettings>
    <add key="LoginUrl" value="~/Login.aspx"/>
  </appSettings>

 

 

撰寫登入網頁命名為 Login.aspx,此頁面繼承 TBLoginPage 類別,並覆寫 ExecuteLogin 方法。在此做登入測試,故在 ExecuteLogin 方法中只判斷登入帳號/密碼為 admin/admin 時即傳回 True,表示登入成功。實際使用時可以在此方法自行連結資料庫或呼叫中間層做身份認識的動作。當按下「登入」鈕時,只需呼叫 TBLoginPage 的 Login 方法傳入帳號/密碼即可。

在網站中任何新增的頁面只要繼承 TBBasePage,當使用者瀏覽該網頁時,就會由機制處理登入驗證的動作,無需在頁面做相關控管。

 

Login.aspx 程式碼

   1:  <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" %>
   2:   
   3:  <html xmlns="http://www.w3.org/1999/xhtml">
   4:  <head runat="server">
   5:      <title>未命名頁面</title>
   6:  </head>
   7:  <body>
   8:      <form id="form1" runat="server">
   9:          <div>
  10:              <table align="center" style="border: #0066cc solid 1px" border="1">
  11:                  <tr>
  12:                      <td align="center" colspan="3" style="height: 20px; color: white; background-color: #3366cc">
  13:                          登入</td>
  14:                  </tr>
  15:                  <tr>
  16:                      <td align="center" style="width: 83px; height: 17px; text-align: right;">
  17:                          帳號
  18:                      </td>
  19:                      <td align="center" style="height: 17px; text-align: left; width: 209px;">
  20:                          <asp:TextBox ID="txtLoginID" runat="server" Width="200px" AutoCompleteType="Disabled"
  21:                              CssClass="username"></asp:TextBox></td>
  22:                  </tr>
  23:                  <tr>
  24:                      <td align="center" style="width: 83px; height: 21px; text-align: right;">
  25:                          密碼
  26:                      </td>
  27:                      <td align="center" style="height: 21px; text-align: left; width: 209px;">
  28:                          <asp:TextBox ID="txtLoginPwd" runat="server" Width="200px" TextMode="Password" AutoCompleteType="Disabled"
  29:                              EnableViewState="False" CssClass="password"></asp:TextBox></td>
  30:                  </tr>
  31:                  <tr>
  32:                      <td colspan="3" style="text-align: right; vertical-align: text-bottom; height: 30px;">
  33:                          <asp:Button ID="btnLogin" runat="server" Text="登入" />
  34:                      </td>
  35:                  </tr>
  36:              </table>
  37:          </div>
  38:      </form>
  39:  </body>
  40:  </html>

 

 

Login.aspx.vb

   1:  Partial Class Login
   2:      Inherits Bee.Web.TBLoginPage
   3:   
   4:      ''' <summary>
   5:      ''' 執行登入驗證。
   6:      ''' </summary>
   7:      ''' <param name="UserID">登入帳號。</param>
   8:      ''' <param name="Password">登入密碼。</param>
   9:      ''' <returns>登入成功傳回 Treu,反之傳回 False。</returns>
  10:      Protected Overrides Function ExecuteLogin(ByVal UserID As String, ByVal Password As String) As Boolean
  11:          If UserID = "admin" AndAlso Password = "admin" Then
  12:              Return True
  13:          Else
  14:              Return False
  15:          End If
  16:      End Function
  17:   
  18:      Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
  19:          Me.Login(txtLoginID.Text, txtLoginPwd.Text)
  20:      End Sub
  21:  End Class

 

 

 

ASP.NET 魔法學院