7

I am facing issues with TempData after Redirect.

public ActionResult LoginCredentials()
{
    // Calling "SetError()" in catch(), if password mismatch.                        
    try{}

    catch()
    {
      return SetError();
    }   
}

public ActionResult SetError()
{
    // set the value of TempData as "true"                        
    TempData["error"] = true;
    return Redirect("/Login");                
}


public ActionResult Index()
{
    ViewData["useError"]= TempData["error"]; // at this point TempData["error"] is null.
    ...
}

In SetError() value of TempData is successfully set as true, issue takes place after "Redirect", value becomes "null" and I can't use it anymore.

user3106445
  • 341
  • 1
  • 4
  • 13
  • Please post your code for controller class here... – Fabjan Jul 27 '15 at 11:15
  • 1
    Whatever I had I have posted it. – user3106445 Jul 27 '15 at 11:22
  • TempData["error"] in the (Index) function will be null because (SetError) function did not excute. – Ala Jul 27 '15 at 11:39
  • Hard to understand what your trying to do here. It should be `return RedirectToAction("SetError);` not `return SetError();`, but then all that method does is a redirect again, so why not just `catch() { TempData["error"] = true; return RedirectToAction("Index); }` in the `LoginCredentials()` method? –  Jul 27 '15 at 12:08
  • @user3106445 I tried doing exactly what you did and it works fine for me, the issue may be something else. – Vibhesh Kaul Jul 27 '15 at 12:43

6 Answers6

7
    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;
    });

turn off options.CheckConsentNeeded = context => true; 

this worked for me

Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
Aziz Nortozhiev
  • 365
  • 5
  • 8
5
  1. maybe the browser is cookieless
  2. the data in a TempDataDictionary object persists only from one request to the next, unless you mark one or more keys for retention by using the Keep method, accoding to your code, if you redirect to login page, and then redirect to index, the value will be null. you can only read it at login page.
aspark
  • 340
  • 2
  • 7
  • I am using TempData once, index is nothing but the view of the controller. – user3106445 Jul 27 '15 at 11:46
  • @user3106445 TempData:Represents a set of data that persists only from one request to the next. https://msdn.microsoft.com/en-us/library/system.web.mvc.tempdatadictionary(v=vs.118).aspx **Redirec to login is the first request, redirect to index from the login is the second request** – aspark Jul 28 '15 at 01:21
  • **I have marked this answer, because now I have redirected directly to index page.** – user3106445 Jul 28 '15 at 04:51
  • So does TempData still become null even if it was never read? – eaglei22 Sep 04 '15 at 14:06
  • I was visiting website on http when was enabled in web.config. – Rasmus-E May 31 '16 at 08:16
  • @user1794106 no, at the end of the request, keys has read will be removed. the question above is redirect to the login page which may clear the session or read the tempdata. then it will be null at the next request – aspark Sep 18 '16 at 07:07
  • This will occur when using the Brave browser. – RugerSR9 Jul 26 '19 at 20:31
2

I find .Net Core incredibly problematic. I had to turn this off in the Configuration

options.CheckConsentNeeded = context => true;

and it worked when I used Redirect to navigate to another page.

However on refreshing the page the TempDate or ViewData lose their value. But when I reassigned it to itself in the "View" it worked:

@{
TempData["somevalue"] = TempData["somevalue"];

}
1

Use RedirectToAction

public ActionResult SetError()
{
// set the value of TempData as "true"                        
   TempData["error"] = true;
   return RedirectToAction("YourViewName");                
 }
Vivek Ranjan
  • 1,432
  • 2
  • 15
  • 37
0

As far as i understand ViewData saves data only after redirect, not when just another Http request occur. So inside Login method(where you redirect to) this ViewData["useError"] have to be available, but Index method is just another method which is executed during another http request.That's why ViewData["useError"] is empty
You can use Session if you want to store data between different Http requests

Disappointed
  • 1,100
  • 1
  • 9
  • 21
0

Setting the cookie as essential in CookieTempDataProviderOptions Solved the issue for me.

services.Configure<CookiePolicyOptions>(options =>
{
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    options.CheckConsentNeeded = _ => true;
    options.MinimumSameSitePolicy = SameSiteMode.Strict;
});
services.Configure<CookieTempDataProviderOptions>(options =>
{
    options.Cookie.IsEssential = true;
});

More info: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.cookietempdataprovideroptions.cookie?view=aspnetcore-7.0

Cathal
  • 1
  • 1