Passing TempData with RedirectToAction

前端 未结 5 1597
夕颜
夕颜 2021-02-13 15:27

Intro: I am a .NET studet trying to learn ASP.NET Core MVC. So please be understanding. I have searched the web for an answer to my problem, but havent found a solution that wor

5条回答
  •  日久生厌
    2021-02-13 16:04

    Providers

    The TempData is using various providers for storing the state. By default the cookie based data provider is used.

    Session is just an alternative

    If your application do not use session I do not see any reason to use it only for TempData store.

    Cookie Consent

    ASP NET Core 2.1 have some new GDPR features based on cookies. By default, data should be stored in cookies only with the user's consent. If the user does not agree with the storing data in cookies, TempData cannot work. This behavior varies across versions of ASP NET Core.

    If you do not want to hold any sensitive data in cookies, you can obviously change the settings.

        app.UseCookiePolicy(new CookiePolicyOptions
        {
            CheckConsentNeeded = context => false
        });
    

    You can set the CookiePolicyOptions separatelly in ConfigureServices as well. It is a quite cleaner.

    Story continues

    We have two kind of data in the cookies. Essential data (needed for running application) and non-essential (some user data). User consent is needed for non-essential data. TempData is non-essential. You can set you TempData as essential and user consent is not needed anymore:

    services.Configure(options => {
       options.Cookie.IsEssential = true;
    });
    

    I highly recommend to think about this before copy / paste.

提交回复
热议问题