http://localhost:50280/WebSite2019-1-7/Login.aspx.vb 一个login控件解决管理员和普通用户登录问题

版权声明: https://blog.csdn.net/eds124/article/details/86072679

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

猜你喜欢

转载自blog.csdn.net/eds124/article/details/86072679