TempData null in asp.net core

Edited:

As Described in https://learn.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.2#tempdata-provider-and-session-state-cookies-arent-essential
by default TempData cookies are removed by the CookiePolicy Middleware. this can be changed by putting this in Startup.ConfigureServices():

// The TempData provider cookie is not essential. Make it essential
// so TempData is functional when tracking is disabled.
services.Configure<CookieTempDataProviderOptions>(options => {
    options.Cookie.IsEssential = true;
}); 

=============================================

Old Answer:

After Migrating to ASP Core 2.1 I had this issue and after working for a day find the solution:

in Startup.Configure()
app.UseCookiePolicy(); should be after app.UseMVC();

Leave a Comment