TempData is null in asp.net core

The TempData provider cookie isn’t essential. CheckConsentNeeded is used to get user permission to enable cookies. If user has denied cookies then TempData cookies will not be provided to browser.

In Asp.Net Core 2.1 to enable TempData cookies disable the CheckConsentNeeded option in Startup.ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
    {
        services.Configure.CookiePolicyOptions(options =>
        {
            // This lambda determines whether user consent for non-essential cookies 
            // is needed for a given request.
            // options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
}

Asp.Net Core 2.2 has provided a new feature to configure the non-essential cookies even user has denied the cookie tracking permission. Add IsEssential property in Startup.ConfigureServices method as below.

public void ConfigureServices(IServiceCollection services)
    {
        services.Configure.CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies 
            // is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.Cookie.IsEssential = true;
            options.MinimumSameSitePolicy = SameSiteMode.None;

        });
}

Leave a Reply

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