Partial Class _Default Inherits System.Web.UI.Page Private sds As SqlDataSource = New SqlD


Partial Class _Default
    Inherits System.Web.UI.Page

    Private sds As SqlDataSource = New SqlDataSource("Data Source=.;Initial Catalog=master;User ID=sa;Password=Abcdefg1", "select count(*) from t_user where username = @username and userpass = @userpass")

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If Request.Cookies("username") Is Nothing Or Request.Cookies("userpass") Is Nothing Then
            Return
        End If

        Dim username As String = HttpUtility.HtmlDecode(Request.Cookies("username").Value)
        Dim userpass As String = HttpUtility.HtmlDecode(Request.Cookies("userpass").Value)

        sds.SelectParameters.Clear()
        sds.SelectParameters.Add("username", username)
        sds.SelectParameters.Add("userpass", userpass)

        Dim i As Integer = DirectCast(sds.Select(DataSourceSelectArguments.Empty), System.Data.DataView)(0)(0)
        If i = 0 Then
            Return
        End If

        Response.Redirect("~/Index.aspx")

    End Sub

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

        Dim username As String = Me.Login1.UserName.Trim().ToLower()
        Dim userpass As String = Me.Login1.Password.Trim().ToLower()
        Dim b As Boolean = Me.Login1.RememberMeSet

        sds.SelectParameters.Clear()
        sds.SelectParameters.Add("username", username)
        sds.SelectParameters.Add("userpass", userpass)

        Dim i As Integer = DirectCast(sds.Select(DataSourceSelectArguments.Empty), System.Data.DataView)(0)(0)
        If i = 0 Then
            Return
        End If

        Response.Cookies("username").Value = HttpUtility.UrlEncode(username)
        Response.Cookies("userpass").Value = HttpUtility.UrlEncode(userpass)

        If b Then
            Response.Cookies("username").Expires = DateTime.Now.AddDays(7) '设置cookie超时时间为7天
            Response.Cookies("userpass").Expires = DateTime.Now.AddDays(7)
        End If

        Response.Redirect("~/Index.aspx") '登录成功跳转
    End Sub
End Class

发布了253 篇原创文章 · 获赞 25 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/eds124/article/details/87828560
sds
今日推荐