Search code examples
asp.net-mvccookiespreferences

How to store user preferences? Cookie becomes bigger


My application (Asp.Net MVC) has great interaction with the user interface (jQuery/js). For example, setting various searches charts, moving the gadgets on the screen and more .. I of course want to keep all data for each user. So that data will be available from any page in the Dumaine and the user will accepts his preferences.

Now I keep all data in a cookie because it did not seem logical asynchronous access to the server each time the user changes something and thet happens a lot.When the user logout from the application I save the cookie to the database.

The Q is how to save the settings back to the db - from the client to the server. because the are a lot of interactin that I want to record. example scanrios: closing widget,moving widget,resizing menues, ordering columens.. I want to record that actions. if I will fire ajax saving rutine for each action ןt will be too cumbersome. Maybe I have no choice.. Maybe I should run an asynchronous saving all of a certain interval seconds.

The problem is the cookie becomes very large. The thought that this huge cookie is attached to each server request makes me feel that my attitude is wrong.

Another problem cookies have size limit. It varies from your browser but I definitely have been close to the border - my cookie easily become 4kb

Is there another solution?


Solution

  • Without knowing your code, have you considered storing the users preferences in a/your database. A UserPreference table with columns for various settings is a possibility.

    You could update it via AJAX/JSON if you had a 'Save Preferences' option, or just update it on postback.

    EDIT 1: After thinking about it, I think having an explicit 'save preferences' button would be beneficial and practical.

    Somewhere on your page, where the use edits the things that generate the cookie, put an button called save, then hook up a jQuery click handler. On click, build a CSV string or another method of storing the preferences for posting back to the server, then use $.post to send it back to an action method in a controller.

    Once there, store it in the database somehow (up to you exactly how), then return a JSON array with a success attribute, to denote whether the preference storing was successful.

    When the page is loading, get the preferences out of the database and perform you manipulation.