【问题标题】:Cookies and session in asp.net how to delete it after the user clicks sign out buttonasp.net中的cookies和session在用户点击退出按钮后如何删除
【发布时间】:2011-10-28 19:28:09
【问题描述】:

我正在创建一个 cookie 和一个会话

if (ValidateUser(txtUserName.Value,txtUserPass.Value) )
        {
            //string useremail = Convert.ToString(txtUserName.Value);
            Session.Add("useremail", txtUserName.Value);
            FormsAuthenticationTicket tkt;
            string cookiestr;
            HttpCookie ck;
            tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now, 
            DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");
            cookiestr = FormsAuthentication.Encrypt(tkt);
            ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
            if (chkPersistCookie.Checked)
            ck.Expires=tkt.Expiration;  
            ck.Path = FormsAuthentication.FormsCookiePath; 
            Response.Cookies.Add(ck);
        }

我正在使用此代码删除 cookie

 protected void SignOut_Click(object sender, EventArgs e)
    {
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
        {
            HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);
            Response.Redirect("Home.aspx");   
        }

   }     

但 cookie 仍然存在,并且在我退出后我能够看到 user.aspx 页面。如何退出,如果是的话,我是否还应该删除会话中的值怎么做

谢谢

【问题讨论】:

标签: c# asp.net session cookies


【解决方案1】:

试试这个

    HttpContext.Current.Session.Remove("useremail");
    HttpContext.Current.Session.Abandon();

【讨论】:

    【解决方案2】:

    当您执行注销时,最好使用Session.Abandon() 结束当前会话。这将确保没有可能泄露的会话信息。

    【讨论】:

    • 所以我不需要删除cookie?
    • 抱歉,还是删除cookie再拨打Session.Abandon()
    猜你喜欢
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2018-10-28
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多