An Introduction To Cookies and Session Stored In TempData In ASP.NET Core

People who used TempData in ASP.NET MVC, probably knows that by default TempData is stored in a Session state. This means that the web application must have sessions enabled. Now, the ASP.NET Core 2.0 gives two TempData providers; a Cookie based and a Session State-based. This blog shows how to use both of them in an application in ASP.NET Core.

Cookies-and-Session-Stored-In-TempData-In-ASP-Dot-Net-Core

Cookie-based TempData

By default, ASP.NET Core 2.0 makes use of cookies based TempData provider. This means values of TempData are stored in cookies present in the client’s machine. Since TempData gest stored in cookies you don’t require Session state through the session state middleware. Storing TempData in cookies could be useful in following situations:

The data being stored is small in size.

A web application is a part of a web farm and you don’t need sticky sessions.

Since cookie based TempData is used by default you don’t have to do any configuration to use it. Just store and receive the TempData key-value pairs as you do. An example is given here:

public IActionResult Index1()

{

TempData[“message”] = DateTime.Now;

return View();

}

public IActionResult Index2()

{

return View();

}

The above code highlights two actions – Index1 and Index2 – of HomeController. The Index1 () action stores a date-time stamp in a dictionary of TempData with a key name of the message. The Index1 view offers a hyperlink to direct to the Home/Index2. The Index2 () action gives back Index2 view. The Index2 view reads the TempData and shows the message to the user.

Index1 view is given below:

<h1>Index 1</h1>

@if (TempData.Peek(“message”) == null)

{

<h2>TempData has not been set.</h2>

}

else

{

<h2>TempData has been set.</h2>

}

<br />

<a asp-controller=”home” asp-action=”index2″>

Go to Index2

</a>

As shown the Razor code finds out whether the message has been stored in TempData or not. It does by using Peek () method of TempData. Since Peek() issued, the key won’t be removed from the TempData. And a message gets displayed in the browser mentioning the status of the message key.

An anchor tag helper offers a hyperlink which points to the Index2 () action of HomeController.

When user clicks on the hyperlink given by Index1. The value of message is sent out in the browser. Markup of Index2 is given below.

<h1>Index 2</h1>

<h2>@TempData[“message”]</h2>

<br />

<a asp-controller=”home” asp-action=”index1″>

Go to Index1

</a>

Index2 reads the TempData dictionary and yields the date-time stamp value in the browser. A hyperlink permits the user to go back to the Index1.

How to know that the TempData is really using the cookies. You need to run the application with F12 tools and locate the cookies option.

The cookie value is Base64 encoded. Since cookies are being used to store TempData all the limitations of cookie storage apply like browser support, size etc.

Session State based TempData

Let’s see how to use Session State based TempData and not the cookie based.

Session state based TempData

To allow Session state based TempData storage you must enable sessions using middleware and you also have to change the default provider. The following code shows how to do it.

public void ConfigureServices(IServiceCollection

services)

{

services.AddMvc().AddSessionStateTempDataProvider();

services.AddSession();

}

public void Configure(IApplicationBuilder app,

IHostingEnvironment env)

{

app.UseSession();

app.UseMvcWithDefaultRoute();

}

The ConfigureServices() calls out AddSessionStateTempDataProvider() to alter the default TempData provider to SessionStateTempDataProvider. It also summons AddSession() to allow in-memory session state for the application. The Configure() summons UseSession() to use the session state middleware.

The HomeController and its both the actions, Index1() and Index2() remains unchanged. On running the application, you can see that the final outcome will be same as before. But there won’t be any CookieTempDataProvider cookie. Instead, a Session cookie.

Session state based TempData comes handy when the application is using session state for other reasons.

That’s it for now!

If you want to upgrade yourself to the basics and concepts of Dot Net Course then our institute would be of great help and support in Dot NET training program

We give a well-structured program for the Best Dot Net Course Training. Among various good institutes of dot net training and placement in Pune, CRB Tech has developed its own identity.

Happy Coding!

Code source: Online database

Don't be shellfish...Buffer this pageEmail this to someoneDigg thisShare on FacebookShare on Google+Share on LinkedInPrint this pageShare on RedditPin on PinterestShare on StumbleUponTweet about this on TwitterShare on Tumblr

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>