Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
Dim username As String
Dim pwd As String
'-------------------------------------
username = Login1.UserName
pwd = Login1.Password
'-------------------------------------
Dim StrConn As String
StrConn = WebConfigurationManager.ConnectionStrings("Database").ConnectionString
Dim Conn As New SqlConnection(StrConn)
Conn.Open()
'-------------------------------------
Dim sqlUserName As String
Dim sqlCheckUser As String
sqlUserName = " SELECT Username,Password FROM member " 'ตรวจสอบ Username และ Password ว่ามีใน database หรือไม่
sqlUserName &= " WHERE (Username ='" & username & "') "
sqlUserName &= " AND (Password ='" & pwd & "' ) "
sqlCheckUser = " SELECT AccessID FROM member " 'ตรวจสอบ User ว่าเป็น Admin หรือ member ธรรมดา
sqlCheckUser &= " WHERE (Username ='" & username & "') "
sqlCheckUser &= " AND (Password ='" & pwd & "' ) "
Dim com As New SqlCommand(sqlUserName, Conn)
Dim com2 As New SqlCommand(sqlCheckUser, Conn)
'-------------------------------------
Dim CurrentName As String
Dim CheckUser As Integer
CurrentName = CStr(com.ExecuteScalar)
CheckUser = CInt(com2.ExecuteScalar)
Response.Write(CheckUser)
If CurrentName <> "" Then
If CheckUser = 1 Then
e.Authenticated = True
Session("UserAuthentication") = username
Session.Timeout = 1
Response.Redirect("admin/AdministratorPage.aspx")
ElseIf CheckUser = 2 Then
e.Authenticated = True
Session("UserAuthentication") = username
Session.Timeout = 1
Response.Redirect("member/UserPage.aspx")
End If
Else
e.Authenticated = False
Session("UserAuthentication") = ""
End If
End Sub
End Class
ในส่วนของไฟล์ AdministratorPage.aspx
<script language="VB" runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Session("UserAuthentication") = "" Then
Response.Redirect("~/Default.aspx")
End If
End Sub
</script>