19.11.16

Asp.net MVC: What to used instead of Session and Cookie


These are my findings while searching that what to use instead of Session and Cookie:

Summary:
1.) If we are creating a "Angular based Application" then 'Local Storage' is the best choice instead of 'Session and Cookie'.
2.) If we are creating a "Mvc application" then we can use 'Local Storage' in the places where data shows on the client side and if we
need data on server side, we need to send that data with previous page URL or send with current page Ajax request.

Also we used Tempdata (instead of Session), QueryString, Hiddenfield , Html5 attributes, Ncache, Redis, Memcached instead of
'Session and Cookie'.


TempData: TempData is session, so they're not entirely different. However, the distinction is easy to understand, because TempData is for
redirects, and redirects only. So when you set some message in TempData and then redirect, you are using TempData correctly. Tempdata can
be used to maintain data between controller actions as well as redirects.

QueryString: Use for small amount of unsecured data.Passing data in query string is not a good choice if those are secure and large. Also
query string too have it's own limit. So you can not pass data after certain limit.

Local Storage: Use for client side. You are not using this data server side, so you don't need a cookie.local Storage is never sent to the server
unlike a cookie.


NCache: NCache is an extremely fast and scalable Open Source distributed cache for .NET applications. Use NCache for database caching,
ASP.NET Session State storage, ASP.NET View State Caching, and much more. NCache provides high performance in-memory ASP.NET session clustering
for web farms that is faster and more scalable than storing them in a database. It is faster because sessions are kept in memory and closer
to the ASP.NET application
Code Demo:
a.) Insert Data:
Product product = new Product();
product.ProductID = 1001;
string key = "Product:" + product.ProductID;
try
{
cache.Add(key, product);
}
b.) Retrieve Data:
string key = "Product:1001";

object result = cache.Get(key);
Redis: Redis is perfect for storing sessions. All operations are performed in memory, and so reads and writes will be fast.
Code Demo:

a.) Insert Data: redisClient.Set(elementKey, "some cached value");

b.) Retrieve Data: redisClient.Get("some cached value");
Memcached: A highly available, high performance ASP.NET session state store provider using Memcached.

Comparision NCache, Redis:
Redis, written in C is completely open source while NCache is written in .NET C# and is open core (which means it also has a paid version).
memcached and redis differ quite a bit.
Redis focuses on performance so most of its design decisions prioritize high performance and very low latencies.

Ncache supports more features than Redis.
Comparision Memcached , Redis:
Memcached is completely in memory and will lose all of its cache in case the server is restarted.
Redis is persistent, and on top of that has a lot more features (like set operations, lists, counters, etc). ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Details links of RND:

Pass data between pages without using Session and Cookie:

1.) Session and Cookies in ASP.NET MVC?
https://gregorybeamer.wordpress.com/2012/11/04/session-and-cookies-in-asp-net-mvc-oh-my/
Ans: Okay, so there is an alternative: Use TempData instead --------------------------------------------------------------------------------------------------------------------------------------------------
2.) using Local Storage

a.) Best Practice for State Management in a Distributed Asp.net MVC 4 Application
http://stackoverflow.com/questions/18161100/best-practice-for-state-management-in-a-distributed-asp-net-mvc-4-application
Ans: You have Local Storage and can use frameworks like AngularJS. Than you can minimize the number of cases, where you'd need a session
state. b.) Local Storage or Session Storage:

http://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies

c.) HTML5 offline storage - Alternative to Session? [closed]
http://stackoverflow.com/questions/11849280/html5-offline-storage-alternative-to-session
Ans: Session data is stored on the server, HTML5 offline storage is stored in the browser. If you are comfortable storing session data in
the browser, sure that will work. If you have sensitive information that should remain on the server however, keep it in sessions.

Disadvantages:
http://stackoverflow.com/questions/16855680/are-there-any-drawbacks-to-using-localstorage-instead-of-cookies
Ans: 1.)If a user disable cookies, localStorage will not work either.
2.)You are not using this data server side, so you don't need a cookie. localStorage is never sent to the server unlike a cookie
3.) The data stored in localStorage and sessionStorage can easily be read or changed from within the client/browser so should not be
relied upon for storage of sensitive or security related data within applications. 3.) using Secure Query String:

a.) MVC Encrypt Query String:
http://stackoverflow.com/questions/895586/encrypting-an-id-in-an-url-in-asp-net-mvc
Ans: you have some sensitive data in the form of a query string and want it encrypted so the end user can't see it. But you need the ability
to decrypt this value in your application to do something with it. 4.) Using Hidden Fields and html 5 Attributes:

http://stackoverflow.com/questions/11770772/how-to-get-html5-attributes-and-values-into-mvc-hiddenfor
Ans: @Html.HiddenFor(x => x.Deleted, new { @class="deleted", data_id=Model.Id }) 5.)
How can I pass parameters to a partial view in mvc 4
http://stackoverflow.com/questions/20799658/how-can-i-pass-parameters-to-a-partial-view-in-mvc-4
Ans: @Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } }); 6.) NCache:

http://www.alachisoft.com/resources/docs/ncache/help/asp.net-sessions.html?mw=MjQw&st=MQ==&sct=MA==&ms=QwAAEAAAAAAAAAACASAG
NCache provides high performance in-memory ASP.NET session clustering for web farms that is faster and more scalable than storing them in a
database. It is faster because sessions are kept in memory and closer to the ASP.NET application
Code: Hashtable allSessionData = cache.GetByTag(new Tag("NC_ASP.net_session_data"));
Ncache: https://ncache.codeplex.com/
7.) Redis: http://stackoverflow.com/questions/10278683/how-safe-it-is-to-store-session-with-redis

Asp.net Redis:
https://blogs.msdn.microsoft.com/webdev/2014/05/12/announcing-asp-net-session-state-provider-for-redis-preview-release/

Integrating Redis in your Asp.Net MVC Project:
http://patrickdesjardins.com/blog/integrating-redis-in-your-asp-net-mvc-project
8.) Memcached

https://github.com/rohita/MemcachedSessionProvider

Which is better Redis, Ncache?
https://www.quora.com/Which-is-better-Redis-or-NCache
CacheManager
http://cachemanager.net/

No comments: