asp.net 中的 Cookie 和会话如何在用户单击注销按钮后将其删除

发布于 2024-11-29 21:52:33 字数 1391 浏览 0 评论 0原文

我正在创建一个 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 页面。如何注销,如果是的话,我是否还应该删除会话中的值,该怎么做,

谢谢

I am creating a cookie and a session

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);
        }

I am using this code to delete the 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");   
        }

   }     

but still the cookie is there and I am able to see the user.aspx page after i sign out. how to sign out and should I also delete the value in the session if so how to do that

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙厌世 2024-12-06 21:52:33

试试这个

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

Try This

    HttpContext.Current.Session.Remove("useremail");
    HttpContext.Current.Session.Abandon();
暮色兮凉城 2024-12-06 21:52:33

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

When you perform a log out it is best to end the current session using Session.Abandon(). This will ensure that there is no session information that could be leaked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文