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.

Tags:

Asp.net How To Write a Cookie and ReadAsp.net How To Remove CookieAsp.net How To Expire a Cookie[Asp.net] Get current Page URL absolute URI and query string parameters.asp net image uploadAsp.net Interview Questions for 2 years or 3 years experianceASP.net File HandlingASp.net 2.0 step by step Membership Provider Sessions In AspnetAsp.net Convert String To ImageAsp.net forms authentication

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.