通过 Cookie 登录





1.00/5 (3投票s)
各种论坛上充斥着关于如何手动实现 Cookie 登录或换句话说如何实现“记住我”的提问。
不同的论坛上充斥着关于如何手动实现 Cookie 登录或换句话说如何实现“记住我”选项的问题。
以下代码将为您提供实现此任务的想法。
使用的控件
1. 文本框,ID = TbUserName
2. 文本框,ID = TbPassword
3. 复选框,ID = CbRememberMe
4. 按钮,ID = BtLogin
5. 链接按钮,ID = lbSignout
------------------如果您使用 VB.Net-------------------------
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'检查浏览器是否支持 Cookie
If Request.Browser.Cookies Then
'检查用户计算机上是否存在名为 PBLOGIN 的 Cookie
If Request.Cookies("PBLOGIN") IsNot Nothing Then
'将用户名和密码传递给 VerifyLogin 方法
Me.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString())
End If
End If
End If
End Sub
Protected Sub BtLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'检查登录时是否选中了“记住我”复选框
If (Me.CbRememberMe.Checked) Then
'检查浏览器是否支持 Cookie
If (Request.Browser.Cookies) Then
'检查用户计算机上是否存在名为 PBLOGIN 的 Cookie
If (Request.Cookies("PBLOGIN") Is Nothing) Then
'创建有效期为 30 天的 Cookie
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30)
'将用户名写入 Cookie
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
'将密码写入 Cookie
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
'如果 Cookie 已存在,则在 Cookie 中写入用户名和密码
Else
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
End If
End If
End If
Me.VerifyLogin(Me.TbUserName.Text, Me.TbPassword.Text)
End Sub
Protected Sub VerifyLogin(ByVal UserName As String, ByVal Password As String)
Try
'如果登录凭据正确
'重定向到用户页面
'否则
'提示用户密码无效
'end if
Catch ex as System.Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub lbSignout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbSignout.Click
'检查用户计算机上是否存在名为 PBLOGIN 的 Cookie
If (Request.Cookies("PBLOGIN") IsNot Nothing) Then
'使 Cookie 失效
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30)
End If
'重定向到登录页面
End Sub
End Class
------------------如果您使用 C#.Net-------------------------
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
//检查浏览器是否支持 Cookie
if (Request.Browser.Cookies)
{
//检查用户计算机上是否存在名为 PBLOGIN 的 Cookie
if (Request.Cookies("PBLOGIN") != null)
{
//将用户名和密码传递给 VerifyLogin 方法
this.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString());
}
}
}
}
protected void BtLogin_Click(object sender, System.EventArgs e)
{
//检查登录时是否选中了“记住我”复选框
if ((this.CbRememberMe.Checked))
{
//检查浏览器是否支持 Cookie
if ((Request.Browser.Cookies))
{
//检查用户计算机上是否存在名为 PBLOGIN 的 Cookie
if ((Request.Cookies("PBLOGIN") == null))
{
//创建有效期为 30 天的 Cookie
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30);
//将用户名写入 Cookie
Response.Cookies("PBLOGIN").Item("UNAME") = this.TbUserName.Text;
//将密码写入 Cookie
Response.Cookies("PBLOGIN").Item("UPASS") = this.TbPassword.Text;
}
//如果 Cookie 已存在,则在 Cookie 中写入用户名和密码
else
{
Response.Cookies("PBLOGIN").Item("UNAME") = this.TbUserName.Text;
Response.Cookies("PBLOGIN").Item("UPASS") = this.TbPassword.Text;
}
}
}
this.VerifyLogin(this.TbUserName.Text, this.TbPassword.Text);
}
protected void VerifyLogin(string UserName, string Password)
{
try
{
//如果登录凭据正确
//重定向到用户页面
//否则
//提示用户密码无效
//end if
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}
}
protected void lbSignout_Click(object sender, System.EventArgs e)
{
//检查用户计算机上是否存在名为 PBLOGIN 的 Cookie
if ((Request.Cookies("PBLOGIN") != null))
{
//使 Cookie 失效
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30);
}
//重定向到登录页面
}
}