Below is the sample code to save n fetch cookie in ASP.NET
Excookie .aspx.cs
Excookie .aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Excookie : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Check if browser accepts cookies.
if (Request.Browser.Cookies)
{
// If the cookie(UpdatedDate) does not exist...
if (Request.Cookies["UpdatedDate"] == null)
{
// Create cookie object.
HttpCookie cookUpdateDate = new HttpCookie("UpdatedDate",
DateTime.Now.ToString());
// Set the expiration .
cookUpdateDate.Expires = DateTime.Now.AddDays(1);
// Add to cookies collection.
Response.Cookies.Add(cookUpdateDate);
// Display message.
Response.Write("This is when you last updated.");
}
else
{
// Fetch the cookie.
HttpCookie cookUpdateDate = Request.Cookies["UpdatedDate"];
// Display a message.
Response.Write("Last Updated Time: " +
cookUpdateDate.Value);
// Update the cookie value on the client.
Response.Cookies["UpdatedDate"].Value =
DateTime.Now.ToString();
//Update the cookie Expiration on the client.
Response.Cookies["UpdatedDate"].Expires =
DateTime.Now.AddDays(1);
}
}
else
{
Response.Write("Cookies has been disabled in the browser .");
}
}
}
Below is the saved cookie file on my machine
very hepful
ReplyDelete