file:///D:\2019-02-16\WebSite\Default.aspx.vb


Partial Class _Default
    Inherits System.Web.UI.Page

    Private username As String = ""
    Private userpass As String = ""
    Private b As Boolean = False
    Private sql As String = "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") IsNot Nothing And Request.Cookies("userpass") IsNot Nothing Then
            Me.username = Server.HtmlEncode(Request.Cookies("username").Value)
            Me.userpass = Server.HtmlEncode(Request.Cookies("userpass").Value)

            Dao.sds.SelectCommand = sql
            Dao.sds.SelectParameters.Clear()
            Dao.sds.SelectParameters.Add("username", Me.username)
            Dao.sds.SelectParameters.Add("userpass", Me.userpass)

            Dim ie As IEnumerable = Dao.sds.Select(DataSourceSelectArguments.Empty)
            Dim dv As System.Data.DataView = ie
            Dim i As Integer = dv(0)(0)

            If i > 0 Then
                Response.Redirect("~/Index.aspx")
            End If
        End If
    End Sub

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
        Me.username = Me.Login1.UserName
        Me.userpass = Me.Login1.Password
        Me.b = Me.Login1.RememberMeSet

        Dao.sds.SelectCommand = sql
        Dao.sds.SelectParameters.Clear()
        Dao.sds.SelectParameters.Add("username", Me.username)
        Dao.sds.SelectParameters.Add("userpass", Me.userpass)

        Dim ie As IEnumerable = Dao.sds.Select(DataSourceSelectArguments.Empty)
        Dim dv As System.Data.DataView = ie
        Dim i As Integer = dv(0)(0)

        If i > 0 Then
            Response.Cookies("username").Value = Server.HtmlEncode(username)
            Response.Cookies("userpass").Value = Server.HtmlEncode(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 If
    End Sub
End Class

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

猜你喜欢

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