开发者

Cookies and session in asp.net how to delete it after the user clicks sign out button

开发者 https://www.devze.com 2023-03-28 12:02 出处:网络
I am creating a cookie and a session if (ValidateUser(txtUserName.Value,txtUserPass.Value) ) { //string useremail = Convert.ToString(txtUserName.Value);

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.FormsCo开发者_如何学编程okieName, 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


Try This

    HttpContext.Current.Session.Remove("useremail");
    HttpContext.Current.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号