How to use cookie expire time? - asp.net

I wrote this code for my login page:
HttpCookie cookie = new HttpCookie("a");
cookie["name"] = (string)ddr["name"];
cookie["phone"] = (string)ddr["phone"];
cookie.Expires = DateTime.Now.AddSeconds(10);
How can I use the cookie expire time in other pages to block users after 10 seconds?

Related

Asp.Net Cookie does persists after browser closing

What i'm basically doing is creating a cookie and using form auth.
This is what i'm using to create the cookie.
Session.Add("username", userName);
Session.Add("password", passWord);
FormsAuthenticationTicket tkt;
string cookiestr;
HttpCookie ck;
tkt = new FormsAuthenticationTicket(1, userName, DateTime.Now,
DateTime.Now.AddMinutes(1), false, "your custom data");
cookiestr = FormsAuthentication.Encrypt(tkt);
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
ck.Path = FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(ck);`
Because i'm logging in to something secure, what I want to happen is when the browser closes to delete the cookie that this site creates. However, that doesnt happen, i'm able to log into the secure section of my site.
What am I missing?
basically if I log into /secure/secure.aspx and then close my browser, i can type in www.myaddress.com/secure/secure.aspx and i'll be logged in. I'm 100% positive that it will deny the user if the user has never logged in.
That behavior is controlled by the browser settings. You can control when the author cookie expires but now when its deleted from the client machine.
One strategy is to set the cookie to expire in X minutes on each request. That way if the user is inactive on the site for more than X minutes, they will have to login again.

Expiring asp.net session cookie

In my logout function, I have
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddYears(-30);
I see that the response contains set-cookie for ASP.NET_SessionId with the proper expiration, but then the browser (Chrome in this case) never actually deletes the cookie.
tiy can try with this code
HttpCookie myCookie = new HttpCookie("ASP.NET_SessionId");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);

asp.net delete cookie

I'm using a cookie containing an encrypted key to use for authentication. What i need is to delete this cookie on logout. As per msdn a cookie cannot be removed from a client's browser, so I tried to set expiry date HttpContext.Current.Request.Cookies["CAuthCookie"].Expires = DateTime.Now.AddDays(-1);, however the cookie remains. Any other ideas?
Try this:(place this in your logout code)
HttpCookie cookie = new HttpCookie("CAuthCookie", "");
cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Set(cookie);

How can I delete a cookie in a particular page?

I am creating a cookie in one page of an ASP.NET application and I want to delete it in another page. How do I do that?
Microsoft: How To Delete a Cookie
You cannot directly delete a cookie on a user's computer. However, you can direct the user's browser to delete the cookie by setting the cookie's expiration date to a past date. The next time a user makes a request to a page within the domain or path that set the cookie, the browser will determine that the cookie has expired and remove it.
To assign a past expiration date on a cookie
Determine whether the cookie exists in the request, and if so, create a new cookie with the same name.
Set the cookie's expiration date to a time in the past.
Add the cookie to the Cookies collection object of the Response.
The following code example shows how to set a past expiration date on a cookie.
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
Note: Calling the Remove method of the Cookies collection removes the cookie from the collection on the server side, so the cookie will not be sent to the client. However, the method does not remove the cookie from the client if it already exists there.
Have you tried expiring your cookie?
protected void btnDelete_Click(object sender, EventArgs e)
{
Response.Cookies["cookie_name"].Expires = DateTime.Now.AddDays(-1);
}
How to: Delete a Cookie
if (Request.Cookies["MyCookie"] != null)
{
HttpCookie myCookie = new HttpCookie("MyCookie");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
First you have to set the expiry date of the cookie to a previous date.
For Example :
HttpCookie newCookie = new HttpCookie("newCookie");
newCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(newCookie);
Now only doing this will not be helpful as the cookie will not be physically removed. You have to remove the cookie.
if (newCookie.Expires < DateTime.Now)
{
Request.Cookies.Remove("newCookie");
}
Here you go. This applies to any page within the solution.

Update cookie expires without any change in the value

How can I simply update the expiry of a cookie on each page request without having any impact on its value ?
'update the User cookie expiration time on every page load
Dim cookieName As String = ConfigKeys.UserCookieName
Dim cookieExpr As String = ConfigKeys.CookieExpiration.ToString
'--get the cookies from request object
Dim userCookie As HttpCookie = HttpContext.Current.Request.Cookies(cookieName.ToUpper())
'--set the expiry date
userCookie.Expires = DateTime.Now.AddMinutes(Integer.Parse(cookieExpr))
'--add the updated cookies back to Response object
HttpContext.Current.Response.Cookies.Add(userCookie)
HttpContext.Current.Response.Cookies["MyCookie"].Expires =
DateTime.Now.AddDays(1)
Or set it to maximum and forget about the expiration:
HttpContext.Current.Response.Cookies["MyCookie"].Expires =
DateTime.MaxValue

Resources