Hi,
Same site cookie attribute set vb.net
Hi,
Same site cookie attribute set vb.net
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Sep 23, 2025, 5:59 AM EDT
In VB.NET (ASP.NET), you can set the
SameSiteattribute of a cookie using theHttpCookieclass orResponse.Cookiesdepending on whether you are using .NET Framework or .NET Core/ASP.NET Core. Here's a concise guide:ASP.NET (.NET Framework)
Dim cookie As New HttpCookie("MyCookie") cookie.Value = "SomeValue" cookie.Expires = DateTime.Now.AddDays(7) cookie.SameSite = SameSiteMode.Strict ' Options: None, Lax, Strict Response.Cookies.Add(cookie)Notes:
SameSiteMode.Strict→ Cookie sent only for same-site requests.SameSiteMode.Lax→ Cookie sent for top-level navigation GET requests.SameSiteMode.None→ Cookie sent in all contexts (requiresSecure = True).If
SameSite=None, you must set Secure = True for modern browsers.For cross-site scenarios (like SSO or third-party login),
SameSite=Noneis needed.Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 23, 2025, 12:09 AM EDT
you’re asking how to set a SameSite cookie attribute in VB.NET (ASP.NET).
Let’s go step-by-step
What is the SameSite Cookie Attribute?
The
SameSiteattribute controls how cookies are sent with cross-site requests to improve security and prevent CSRF attacks.Possible values:
None→ cookie is sent in all requests (must also haveSecure=Truefor HTTPS)Lax→ cookie sent only on top-level GETs (default for most browsers)Strict→ cookie sent only for same-site requestsHow to Set
SameSiteAttribute in VB.NETOption 1: Set it directly when creating a cookie
Dim cookie As New HttpCookie("UserSession") cookie.Value = "Sandhiya123" cookie.HttpOnly = True cookie.Secure = True ' Required when SameSite=None cookie.SameSite = SameSiteMode.None cookie.Expires = DateTime.Now.AddHours(1) Response.Cookies.Add(cookie)Option 2: Set globally in
web.config(for all cookies)You can enforce
SameSitebehavior in your Web.config:Option 3: Fix cookies generated by
FormsAuthenticationIf you’re using ASP.NET Forms Authentication, set SameSite for the auth cookie in
Global.asax:Sub Application_PostAuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs) Dim authCookie = Response.Cookies(FormsAuthentication.FormsCookieName) If authCookie IsNot Nothing Then authCookie.SameSite = SameSiteMode.None authCookie.Secure = True End If End SubOption 4: Modify cookies dynamically (for older .NET versions)
If your .NET Framework version doesn’t support
.SameSiteproperty (e.g., before 4.7.2),you can append the attribute manually:
Recommended Settings (for SSO apps)
If your app uses Single Sign-On (SSO) or any cross-domain login, use:
Otherwise, for a normal internal app (no external redirect), use:
Quick Example (Full VB.NET Code)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim cookie As New HttpCookie("MyLoginCookie") cookie.Value = "User123" cookie.Expires = DateTime.Now.AddHours(2) cookie.HttpOnly = True cookie.Secure = True cookie.SameSite = SameSiteMode.None Response.Cookies.Add(cookie) End Sub