Hi,
I am creating a cookie using the following code:
Dim iharyanacookie As HttpCookie
iharyanacookie = New HttpCookie("username", uid.Text)
Response.Cookies.Add(iharyanacookie)
But how can I delete or destroy a cookies. I using the following code about cookies is still there and working.
Response.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Remove("iharyanacookie")
Response.Redirect("http://www.iharyana.com")
Basically the whole page code is:
Imports System.Data.SqlClient
Partial Class WebUserControl
Inherits System.Web.UI.UserControl
Login Button:
Protected Sub login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles login.Click
login_error.Text = ""
Dim conn1 As SqlConnection
Dim comm1 As SqlCommand
Dim reader1 As SqlDataReader
Dim connstring1 As String
If reader1.Read Then
Dim pass As String
pass = reader1.Item("password")
If (pass = password.Text) Then 'create cookies
Dim iharyanacookie As HttpCookie
iharyanacookie = New HttpCookie("username", uid.Text)
Response.Cookies.Add(iharyanacookie)
Response.Redirect("http://www.iharyana.com")
Else
login_error.Text = "Invalid Login ID or Password! Try Again"
End If
End If
reader1.Close()
conn1.Close()
End Sub
Page Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim iharyanacookie As HttpCookie
iharyanacookie = Request.Cookies("username")
If iharyanacookie Is Nothing Then
Protected Sub logout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles logout.Click Response.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Remove("iharyanacookie")
Response.Redirect("http://www.iharyana.com")
Protected Sub logout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles logout.Click Response.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Remove("iharyanacookie")
Deleting a cookie—physically removing it from the user's hard disk—is a variation on modifying it. You cannot directly remove a cookie because the cookie is on the user's computer. However, you can have the browser delete the cookie for you. The technique
is to create a new cookie with the same name as the cookie to be deleted, but to set the cookie's expiration to a date earlier than today. When the browser checks the cookie's expiration, the browser will discard the now-outdated cookie. The following code
example shows one way to delete all the cookies available to the application:
</div>
Dim aCookie As HttpCookie
Dim i AsIntegerDim cookieName AsStringDim limit AsInteger = Request.Cookies.Count - 1
For i = 0 To limit
cookieName = Request.Cookies(i).Name
aCookie = New HttpCookie(cookieName)
aCookie.Expires = DateTime.Now.AddDays(-1)
Response.Cookies.Add(aCookie)
Next
Object reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 72: Response.Cookies.Remove("iharyanacookie") Line 73: Line 74: Request.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30) Line 75: Request.Cookies.Remove("iharyanacookie") Line 76: Response.Redirect("http://www.iharyana.com")
Dim iharyanacookie As HttpCookie
iharyanacookie = New HttpCookie("username", "abc")
iharyanacookie.Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Add(iharyanacookie)
Response.Redirect("http://www.iharyana.com")
Thanks for all your help i just have one suggestion that without creating a new cookie with same name we can also take instance of already existing cookie[As we are going to delete a cookie so it must exist :)] and then set its expires date and set it back
in response...for PoC i have created one static function which you can use by just passing the cookie name to delete. Here it is :
/// <summary>
/// This function will be used to remove
/// cookies value
/// </summary>
/// <param name="key"></param>
/// <returns>Cookies value</returns>
public static void RemoveCookie(string key)
{
//get cookies value
HttpCookie cookie = null;
if (HttpContext.Current.Request.Cookies[key] != null)
{
cookie = HttpContext.Current.Request.Cookies[key];
//You can't directly delte cookie you should
//set its expiry date to earlier date
cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
Let me know if you need further assistance. I will be happy to help you.
satender88
Member
16 Points
46 Posts
how to delete cookies?
Mar 01, 2008 11:08 AM|LINK
Hi,
I am creating a cookie using the following code:
Dim iharyanacookie As HttpCookie
iharyanacookie = New HttpCookie("username", uid.Text)
Response.Cookies.Add(iharyanacookie)
But how can I delete or destroy a cookies. I using the following code about cookies is still there and working.
Response.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Remove("iharyanacookie")
Response.Redirect("http://www.iharyana.com")
Basically the whole page code is:
Imports System.Data.SqlClient
Partial Class WebUserControl
Inherits System.Web.UI.UserControl
Login Button:
Protected Sub login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles login.Click
login_error.Text = ""
Dim conn1 As SqlConnection
Dim comm1 As SqlCommand
Dim reader1 As SqlDataReader
Dim connstring1 As String
connstring1 = ConfigurationManager.ConnectionStrings("iharyana").ConnectionString
conn1 = New SqlConnection(connstring1)
Dim users As String
users = "select password from usermain where username = @username"
comm1 = New SqlCommand(users, conn1)
comm1.Parameters.Add("@username", Data.SqlDbType.VarChar, 20)
comm1.Parameters("@username").Value = uid.Text
conn1.Open()
reader1 = comm1.ExecuteReader
If reader1.Read Then
Dim pass As String
pass = reader1.Item("password")
If (pass = password.Text) Then
'create cookies
Dim iharyanacookie As HttpCookie
iharyanacookie = New HttpCookie("username", uid.Text)
Response.Cookies.Add(iharyanacookie)
Response.Redirect("http://www.iharyana.com")
Else
login_error.Text = "Invalid Login ID or Password! Try Again"
End If
End If
reader1.Close()
conn1.Close()
End Sub
Page Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim iharyanacookie As HttpCookie
iharyanacookie = Request.Cookies("username")
If iharyanacookie Is Nothing Then
Else
user.Text = "Welcome back <b>" & iharyanacookie.Value & "</b>"
uid.Visible = False
pass.Visible = False
password.Visible = False
login.Visible = False
forgot.Visible = False
sep.Visible = False
reg.Visible = False
my.Visible = True
logout.Visible = True
End If
End Sub
Logout Button:
Protected Sub logout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles logout.Click
Response.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Remove("iharyanacookie")
Response.Redirect("http://www.iharyana.com")
End Sub
End Class
Pls pls help.
ivanatanasov
Participant
1792 Points
335 Posts
Re: how to delete cookies?
Mar 01, 2008 12:06 PM|LINK
hi boy,
Logout Button:
Protected Sub logout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles logout.Click
Response.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Remove("iharyanacookie")
Request.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Request.Cookies.Remove("iharyanacookie")
Response.Redirect("http://www.iharyana.com")
End Sub
End Class
My blog is here.
Please remember to 'Mark as Answer' if this post answered your question!
gopalanmani
Star
7826 Points
1320 Posts
Re: how to delete cookies?
Mar 01, 2008 05:25 PM|LINK
hi,
Deleting Cookies
<div class=subsection>Deleting a cookie—physically removing it from the user's hard disk—is a variation on modifying it. You cannot directly remove a cookie because the cookie is on the user's computer. However, you can have the browser delete the cookie for you. The technique is to create a new cookie with the same name as the cookie to be deleted, but to set the cookie's expiration to a date earlier than today. When the browser checks the cookie's expiration, the browser will discard the now-outdated cookie. The following code example shows one way to delete all the cookies available to the application:
</div>Gopalan Mani
My Tech blog
satender88
Member
16 Points
46 Posts
Re: how to delete cookies?
Mar 02, 2008 02:19 PM|LINK
Hi ivanatanasov,
It is showing following error:
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 72: Response.Cookies.Remove("iharyanacookie")Line 73:
Line 74: Request.Cookies("iharyanacookie").Expires = DateTime.Now.AddYears(-30)
Line 75: Request.Cookies.Remove("iharyanacookie")
Line 76: Response.Redirect("http://www.iharyana.com")
Source File: d:\hosting\satender88\login.ascx.vb Line: 74
Stack Trace:
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
satender88
Member
16 Points
46 Posts
Re: how to delete cookies?
Mar 02, 2008 02:40 PM|LINK
Hi.
This works:
Dim iharyanacookie As HttpCookie
iharyanacookie = New HttpCookie("username", "abc")
iharyanacookie.Expires = DateTime.Now.AddYears(-30)
Response.Cookies.Add(iharyanacookie)
Response.Redirect("http://www.iharyana.com")
Thanks gopalanmani, thanks a ton
klpatil
Participant
1290 Points
245 Posts
Re: how to delete cookies?
Jun 03, 2009 05:04 AM|LINK
Hi,
Thanks for all your help i just have one suggestion that without creating a new cookie with same name we can also take instance of already existing cookie[As we are going to delete a cookie so it must exist :)] and then set its expires date and set it back in response...for PoC i have created one static function which you can use by just passing the cookie name to delete. Here it is :
/// <summary> /// This function will be used to remove /// cookies value /// </summary> /// <param name="key"></param> /// <returns>Cookies value</returns> public static void RemoveCookie(string key) { //get cookies value HttpCookie cookie = null; if (HttpContext.Current.Request.Cookies[key] != null) { cookie = HttpContext.Current.Request.Cookies[key]; //You can't directly delte cookie you should //set its expiry date to earlier date cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); } }HTH
-Kiran
For more solution like this my blog is here
sandy060583
Star
8714 Points
1624 Posts
Re: how to delete cookies?
Jul 03, 2009 11:19 AM|LINK
here is the code to create the cookie & delete it...
//Create Cookie
HttpCookie aCookie = new HttpCookie("AdminInfo");
aCookie.Values["userName"] = txtUsername.Text;
aCookie.Values["Password"] = txtPassword.Text;
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(aCookie);
//Delete Cookie
HttpCookie aCookie = new HttpCookie("AdminInfo");
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
Hope this will help u..
Ramani Sandeep (My Blog)
(MCTS, MCC-2011)
AsifAshraf
Member
205 Points
103 Posts
Re: how to delete cookies?
Feb 09, 2010 04:52 AM|LINK
public void ExpireCookies()
{
for(int i=0; i < Request.Cookies.Count; i++)
{
HttpCookie aCookie = Request.Cookies[i];
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}
}