Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Private S

Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Private Shared ReadOnly cs As String = "Data Source=.;Initial Catalog=master;User ID=sa;Password=Abcdefg1"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        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)
        Using con As SqlConnection = New SqlConnection(_Default.cs)
            con.Open()
            Using cmd As SqlCommand = New SqlCommand("select count(*) from t_user where username = @username and userpass = @userpass", con)
                cmd.Parameters.AddWithValue("username", username)
                cmd.Parameters.AddWithValue("userpass", userpass)
                Dim i As Integer = cmd.ExecuteScalar()
                If i = 0 Then
                    Return
                End If
            End Using
        End Using
        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
        Using con As SqlConnection = New SqlConnection(_Default.cs)
            con.Open()
            Using cmd As SqlCommand = New SqlCommand("select count(*) from t_user where username = @username and userpass = @userpass", con)
                cmd.Parameters.AddWithValue("username", username)
                cmd.Parameters.AddWithValue("userpass", userpass)
                Dim i As Integer = cmd.ExecuteScalar()
                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 Using
        End Using
    End Sub
End Class

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

猜你喜欢

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