How can we access sessionStorage in server side using MVC?

13.4k Views Asked by At

i am saving some of the users insensitive data in the window.sessionStorage. this data is specific to the browsers tab, now i want to access the data which i have stored in the session storage from server side, how can i do that.

2

There are 2 best solutions below

0
J.P. On

You can't. sessionStorage is something that resides within the browser, on the client machine. ASP.NET MVC resides on your server. You can't simply 'pull' client-side browser data towards your server, the browser has to send it to you.

Using JavaScript, you can read whatever is in the session storage and then make an AJAX request to send it to ASP.NET MVC.

let sessionData = sessionStorage.yourData;
this.http.post('api/wherever', sessionData).subscribe(response => { ... });

Another option is to not store the data in the session storage but inside a cookie. Cookie data gets passed along with every HTTP request that goes out to your server. That way, the data will be immediately accessible in ASP.NET MVC.

0
DelRay On

Create an asp.net hidden field like this one,

Page.ClientScript.RegisterStartupScript(this.GetType(), "", "$('#MainContent_hiddenFieldID').val(sessionStorage.getItem('sessionStorageVariableName'));", true);

then simply reference the hidden field on your serverside code.