大多數人的網頁空間是使用租賃的方式,市面上 SQL SERVER主機的租金非常昂貴,一般使用者通常無法負擔。ASP.NET 2.0 的帳號及密碼管理預設為 ASPNETDB.MDF,這是 SQL SERVER 才能進行的方式;如果能夠以 ACCESS 資料庫來管理帳號及密碼,將可節省大量網站建置成本。
在VS2005中建立網站(AccessLogin),拖曳 Login 控制項到設計視窗,控制項名稱為 Login1。
在 AccessLogin 網站 app_data 資料夾建立 Access 資料庫,內含登入資料(例如:db1.mdb 資料庫中 table1 資料表中有下列兩筆登入資料)
在 Default.aspx.vb 中輸入下列程式碼:
Protected Sub Login1_Authenticate1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
'在Access資料庫中驗證登入者
Dim ads As New AccessDataSource("~/app_data/db1.mdb", "")
Dim SqlStr As String = "select * from table1 where name='"
SqlStr = SqlStr & sender.UserName
SqlStr += "' and passwd='" + sender.Password + "'"
ads.SelectCommand = SqlStr
Dim dv As Data.DataView
dv = ads.Select(New DataSourceSelectArguments)
'如果驗證通過
If dv.Count <> 0 Then
'系統記錄已登入訊息
FormsAuthentication.RedirectFromLoginPage(sender.userName, True)
Response.Redirect("main.aspx") ' 導向登入後網頁
Else
Response.Redirect("Default.aspx") ' 導向登入網頁
End If
End Sub
建立 main.aspx 網頁做為登入後的主網頁。
下載範例檔案