Access web storage from server side - possible? - c#

I've stored some strings in web storage (session and/or local), and am wondering if it is possible to check for such stored strings on page load or init on the server-side (asp.net c# in my case)... So, for example, I will know not to re-fetch data from the db and use what is already resident in the browser from the last page load.

No, that's not possible. sessionStorage lives on the client. If you want to access it on the server you will have to write javascript that reads the value, puts it in a hidden field so that it is sent to the server. Or javascript that will read the value from the storage and redirect to the server passing it as query string parameter. There's absolutely no way for the server accessing directly this storage. That's one of the drawbacks of sessionStorage vs cookies.

Related

Blazor: Using local json file as storage

I'm writing small client-side blazor app. To avoid using any DB (and creating whole API for it), as I have basically just a list of single class objects, I wanted to use json file.
I know I can read from file:
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
But when I modify this "forecasts" list and try to do something like this:
await Http.PostAsJsonAsync("sample-data/weather.json", forecasts);
or
await Http.PostAsJsonAsync<WeatherForecast[]>("sample-data/weather.json", forecasts);
It is not saving changes. I checked in browser and it is doing a POST with correct data, but receives 404 in return. Is it possible to save such a data after modification?
What you are trying to do is to change a file on server from a application that lives on the users browser. When the user visits you blazor site all of the files (except the ones that are set for lazy loading) are downloaded into the user browser. After the applications lives and runs in the users browser.
If you don't need any concurrency check and real database capabilities you should build some simple server that allows static file access or other option is to use simple blob access from AWS S3 or Azure Blob Storage.
You can look up here the difference between client and server side Blazor.
If you need to share persistent data between multiple clients you will need to implement a server or service that will allow this for you if you use client side Blazor.

Masking 1000 ajax post request with better performance

We have an application that is installed on some 600 odd servers. This application exposes a web api which gets me version information of the application.
My requirement is to display the version of application on each of the server. I have achieved this in asp.net application by:
Writing a web method in aspx.cs page with server name as parameter. This method will build web api URL, invoke web api, get response, build a object and return as json string.
I have written an jquery ajax post request for each server name to the above method. On success, built a html table row and append it to table. so that as and when we get response it is shown to user.
This works absolutely fine for say 30-40 servers. But when it increases, it takes lot of time to process all requests (30 - 40 mins). And with multiple users using this asp.net app, we start getting error.
Is there a any other method to achieve this faster and for multiple users without errors?
How often is this information changed? If this data is rarely updated, I suggest the following.
Make every of servers to save this information into a shared database table when the server starts. If the application version can be changed while the server is operating, update a corresponding record in a database.
When your service gets information about the application version, get it once from the database table instead of requesting it directly from every server.

Where is the Session stored?

I ask to my friend, where is session stored? At Server or Browser?
He said, at the server.
Then I said "I think things saved at server called cache".
Then, I go to google search reading article, but I found no specific correct answer, at MSDN too, no specific answer.
Usually it is saved in the server's memory, but you can also have a database backed cache. It is never cached on the client, since it can contain information that shouldn't be available to the user, like the password to your database.
The full list of places where you can save the session state can be found on MSDN:
InProc mode, which stores session state in memory on the Web server. This is the default.
StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
Custom mode, which enables you to specify a custom storage provider.
Off mode, which disables session state.
Session itself is stored on the server side.Each browser accessing the server will get from the server unique Session ID. This Session ID browser sends to each page requested to the same server.
Session
So on client (browser) side, only Session ID is stored in the browser cookie.
(this is default behavior, when session cookies are enabled in the browser settings... there is also a technique called "URL rewriting" to embed Session ID as URL query parameter, each time the server is called, enabling the application to work even if browser session cookies are disabled)
For more information go through this http://ejvyas.blogspot.in/2010/02/where-is-stored-is-it-in-browser-or-at.html
Quick answer: on both sides.
Previously, the session was stored on a server side. This approach implies that you have to go to a server side each time you create or validate session. The session also have to be replicated for all the web-servers. These things can sufficiently harm the performance.
The another one oldschool way to store the session data as a cookie (as it is mentioned in answers). The obvious flow is that cookies has 4kb limit.
In order to overcome it, the World Wide Web Consortium defined the client side sessionStorage, that can be instantly added or validated and does not demand to replicate the data between web-servers.
It can be seen in browser's Dev Tools / Application tab. For example, this is how the session for my Facebook page looks like:

Where does HttpRuntime.Cache stores data?

I am trying to implement a web application with Caching features.The reason I need caching is for is that we have an application which lets users access online courses. Now, Once the user logs in I validate him against our database. Once validated I want to store the user ID and course ID in cache for 20 mins so that if he requests it again I can retrieve values from cache foe both user id and course ID and if valid provide him access
For some reasons I can't use Session variables in this application so they are not an option.
Now, the caching code in my application is inside a HTTP Handler(.ashx file). Now I tried calling the cache object like you do for a aspx page but I could not, probably because it's a handler and not a webpage.
So,
Cache.Insert("Id", 123);
will not work in Handler. So then I tried HTTPRuntime.cache. But after doing some research I found out the HTTPRuntime.cache is common to the whole application. I don't completely understand "Whole application". Does it mean that it is shared by all the users on different computers accessing our applications? or does it mean it's shared by all the users on one computer accessing our application. Because if it is the latter I am OK with it.
So is HTTPRuntime.cache a good way to cache data for one browser(or one computer) or is there a different and better way to implement Browser caching to store data?
Cache is stored in web server memory.
You should understand the differences between Viewstate, Cache and Session

Dropping a session variable (or the value of one) into a client's local application

I am trying to build a system where, when the user logs in to an online ASP.NET site, the User ID (that is placed in a session variable) is then stored on to a local application (c# application) for later use.
So far, the only way I can think of doing this is, when the user logs in, the User ID is stored in a text file on the client's machine. But, this would not be an ideal solution, as this means the local client application must then check the file to make sure the contents have not changed (almost every second as it is important that the client Application always has the correct User ID).
Any suggestions?
When you say "the User ID is stored in a text file on the client's machine" I deduce you mean cookies, because you simply can´t store files on client machines via web applications, unless there is some sort of ActiveX control involved.
You can perfectly store a cookie with the User Id on the client and access it with your console app, but this is not very reliable, as the user can have cookies disabled or he can clean the cookies folder and also because different browsers use different folders for storing cookies.
So my choice would rather be a storing the current logged users in a database and make the console app poll that info through a WCF service.
If you don´t want to use a Database, store an XML file on the server that could act as your database and use for example LINQ to XML to retrieve the data via that WCF service.
Other option we can equate instead of polling the info you could use WCF Duplex Services and make the WebService push that info to the client apps once a user logs in.

Categories