1. 程式人生 > >http://localhost:50280/WebSite2019-1-7/Login.aspx.vb 一個login控制元件解決管理員和普通使用者登入問題

http://localhost:50280/WebSite2019-1-7/Login.aspx.vb 一個login控制元件解決管理員和普通使用者登入問題


Partial Class Login
    Inherits System.Web.UI.Page

    Protected Sub Login1_Authenticate(sender As Object, e As AuthenticateEventArgs) Handles Login1.Authenticate
        Dim username As String = Me.Login1.UserName.Trim().ToLower() '獲取使用者名稱
        Dim password As String = Me.Login1.Password.Trim().ToLower() '獲取密碼
        Dim b As Boolean = Me.Login1.RememberMeSet '是否是管理員登入
        Dim sds As SqlDataSource = New SqlDataSource("Data Source=.;Initial Catalog=mydata;User ID=sa;Password=Abcdefg1", "select count(*) from t_user where username = @username and userpass = @password")
        If b Then '如果是管理員進入下面這一句
            sds.SelectCommand = "select count(*) from t_root where rootname = @username and rootpass = @password"
        End If
        sds.SelectParameters.Add("username", username)
        sds.SelectParameters.Add("password", password)
        Dim ie As IEnumerable = sds.Select(DataSourceSelectArguments.Empty) '執行查詢
        Dim dv As System.Data.DataView = CType(ie, System.Data.DataView) '轉換成DataView
        Dim o As Object = dv(0)(0) '取出第一行第一列
        Dim s As String = o.ToString() '轉成字串
        If s.Equals("0") Then '0代表沒有
            Response.Write("登入失敗")
            Return
        End If
        If b Then
            Session("rootname") = username
            Session("rootpass") = password
            Response.Redirect("~/Root/Default.aspx") '管理員跳轉
            Return
        End If
        Session("username") = username
        Session("userpass") = password
        Response.Redirect("~/Default.aspx") '普通使用者跳轉
    End Sub

    Protected Sub Login1_Init(sender As Object, e As EventArgs) Handles Login1.Init
        Me.Login1.RememberMeText = "管理員登入" '修改控制元件顯示的字
    End Sub
End Class