I'd like to know how VB.NET handles cookies

Falanga, Rod, DOH 290 Reputation points
2025-06-04T21:29:07.1866667+00:00

I maintain an old VB.NET WebForms app. I'm converting it to C# and updating it. However, when it comes to converting how the old app handled browser cookies and how to make the same thing work in a C# Blazor app, I am not sure how to proceed. Here's a snippet that illustrates what I mean. It sets two subparts and assigns an expiration date:

Protected Sub StoreStaffInfoInCookie()
    Response.Cookies("StaffInfoCookie")("ClinicID") = ddlClinic.SelectedValue
    Response.Cookies("StaffInfoCookie")("JobID") = ddlJob.SelectedValue
    Response.Cookies("StaffInfoCookie").Expires = DateTime.Now.AddDays(366)
End Sub

The way I think of it is it is an array. At least that's the way VB.NET is handling here. However, in the Blazor/C# code it looks to me like it separates "StaffInfoCookie" from "ClinicID" using a semicolon. Under the hood, is that what VB.NET is doing?

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 245 Reputation points Microsoft External Staff
    2025-07-10T07:36:40.8366667+00:00

    Hi,

    In VB.NET Response.Cookies("StaffInfoCookie")("ClinicID") creates a cookie with subkeys. In C# Web Forms, you can do the same like this:

    HttpCookie cookie = new HttpCookie("StaffInfoCookie");

    cookie["ClinicID"] = ddlClinic.SelectedValue;

    cookie["JobID"] = ddlJob.SelectedValue;

    cookie.Expires = DateTime.Now.AddDays(366);

    Response.Cookies.Add(cookie);

    In Blazor, cookies must be handled using JavaScript (via JS Interop), because direct cookie access isn't available in C#.

     

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 245 Reputation points Microsoft External Staff
    2025-07-16T06:49:03.03+00:00

    Hope you're doing well , we're following up on the ticket. Please let us know issue persist are resolved.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.