Asp.net page specific cookie

Asp.net page specific cookie

You want to set values in cookie, which is valid for page path "/mysubdirectory". Here is example in Asp.net to set page path specific cookie.

Asp.net C# cookie path example.

protected void Page_Load(object sender, EventArgs e)
{

HttpCookie UserNameCookie = new HttpCookie("UserName");
UserNameCookie.Value = "Satalaj";
UserNameCookie.Path = "/web.Demo";

Response.Cookies.Add(UserNameCookie);

}
protected void Button1_Click(object sender, EventArgs e)
{
string username = Request.Cookies["UserName"].Value;

Response.Write("Retrived user name from cookie is: " + username);
}

Asp.net VB.net Cookie path Example.


Protected Sub Page_Load(sender As Object, e As EventArgs)

Dim UserNameCookie As New HttpCookie("UserName")
UserNameCookie.Value = "Satalaj"
UserNameCookie.Path = "/web.Demo"

Response.Cookies.Add(UserNameCookie)

End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim username As String = Request.Cookies("UserName").Value

Response.Write("Retrived user name from cookie is: " + username)
End Sub

What you need to do is, just set path of instance of Http Cookie.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5