Darryl Armstrong

Darryl Armstrong

  • NA
  • 20
  • 6.9k

Blazor App - Write and Read Cookies

Dec 3 2019 9:29 AM
Hi, I am writing my first server sided Blazor App using core 3.0.  I have a simple request, but I cannot work out how to do it.
 
Originally all i wanted to do was use a Session Variable to save some data that I could then use on a different page.  I have since done a lot of reading and have found that you cannot do this.  Instead I decided writing a session cookie to do it will be ok in my example and thought it would be easy to do, but I cannot get that to work either.
 
If i create a blazor app with no authentication, using the standard template app can someone please provide me with some code that would write a session cookie when pressing the increment counter button on the counter tab and then read that cookie in on the Fetch Data tab and show it.
 
I cannot believe it is so hard.  It seems easy to do in non Blazor Applications from what i have read. The below would work:
 
CookieOptions option = new CookieOptions();
option.Expires = DateTime.Now.AddDays(3);
Response.Cookies.Append("TestApp", "123", option);
//read cookie from Request object
string cookieValueFromReq = Request.Cookies["TestApp"];
 
 
The problem seems to be the code behind page does not recognise the response object.  
 
Thank you in advance for your help.
 
Daz 

Answers (1)