读书人

怎样在asp.net+c#中删除cookies(用户登

发布时间: 2012-01-13 22:43:29 作者: rapoo

怎样在asp.net+c#中删除cookies(用户登录信息)?
我在用户登录时用 Session[ "User_id "]=dr[ "User_id "];Session[ "user_power "]=dr[ "User_power "]记录登录的用户名及密码。当正常登录进来后,一切正常,但是,当退出时,仍然可以对其它页面查行操作:就是我点IE工具栏上的后退,还可以直接回到其它页面去,跳过了输入用户及密码。请问高手,应该如何才能正常退出,就是退出时,不可以再对其它页面进行操作操作。谢谢!
本人的想法是能否在页面每次加载的时候,自动清空cookies或者session的所有值。
由于本来是新手,希望高手能够详细的帮我写出代码或者进行说明,再次感谢!
以下是我的页面代码:
namespace sms
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{

}
#endregion
protected void Btn_enter_Click(object sender, System.EventArgs e)
{
string strconn= ConfigurationSettings.AppSettings[ "dsn "];
SqlConnection cn=new SqlConnection(strconn);
cn.Open();
string strsql= "select * from users where User_id= ' "+Tbx_userid.Text+ " 'and User_password= ' "+Tbx_userpwd.Text+ " ' ";
SqlCommand cm=new SqlCommand(strsql,cn);
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{ Session[ "User_id "]=dr[ "User_id "];
Session[ "user_power "]=dr[ "User_power "];
if((int)Session[ "User_power "]==0)
{
Response.Redirect( "query.aspx ");
}
else
{
Response.Redirect( "student.aspx ");
}
}
else
{
Lbl_note.Text= "对不起,登陆失败! ";
}
cn.Close();
}

}
}

[解决办法]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Quit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//清除客户端Cookies
System.Web.Security.FormsAuthentication.SignOut();
Response.Write( " <script language= 'javascript '> window.top.location= 'Default.aspx '; </script> ");
}
}
这是Quit页面的代码,退出时用一个Button链接到此页面就行了
------解决方案--------------------


//清除cookies
if(HttpContext.Current.Request.Cookies[ "UserId "]!=null)
{
HttpCookie cookie= Request.Cookies[ "UserId "];
cookie.Expires=DateTime.Today.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
Response.Write( " <script> parent.window.location.href= 'default.aspx ' </script> ");

读书人网 >asp.net

热点推荐