<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Private Shared conn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(Dao.ConnectionString)
Private Shared comm As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("select count(*) from t_user where username = @username and userpass = @userpass", conn)
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 '获取用户名
Dim userpass As String = Me.Login1.Password
comm.Parameters.Clear() '删除原有参数
comm.Parameters.AddWithValue("username", username) '预处理
comm.Parameters.AddWithValue("userpass", userpass)
Dim o As Object = comm.ExecuteScalar() '查询第一行第一列
If o Then
Response.Cookies("username").Value = username '登录成功写入cookie
Response.Cookies("userpass").Value = userpass
Dim b As Boolean = Me.Login1.RememberMeSet '获取记住我
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
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If conn.State = Data.ConnectionState.Closed Then '页面型单例模式保证一个conn链接
conn.Open()
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server">
</asp:Login>
</div>
</form>
</body>
</html>
file:///E:\WebSite\Default.aspx单例模式
最新推荐文章于 2020-06-03 11:23:31 发布