TempData null after RedirectToAction

Paul Marangoni 126 Reputation points
2024-02-03T18:53:51.7633333+00:00

I am struggling with some very strange behavior. I have TempData working fine in a test situation.

public async Task<IActionResult> Test()
{
    TempData["TEST"] = "HELLO"; 
    return RedirectToAction("Test2");
}
 [HttpGet]
 public IActionResult Test2()
 {
     var tempData = TempData["TEST"]; //TempData is available here...
     return View();
 }

But in another part of my code, TempData is null after redirection:

[HttpGet]
public async Task<IActionResult> DoSomething(int? u, string? e, string? p, int? n)
{
    if (u is null || e is null || n is null)
    {
        return RedirectToAction("Index", "Home");
    }
    else
    {
        var user = await _userManager.FindByIdAsync(u.ToString());
        if (user is null)
        {
            return RedirectToAction("Index", "Home");
        }
        else if (user is not null && !user.Email!.Equals(e, StringComparison.CurrentCultureIgnoreCase))
        {
            return RedirectToAction("Index", "Home");
        }
        else if (user is not null && p is null) 
        {
            TempData["UserID"] = user.Id.ToString();
            return RedirectToAction("Preview", "Home", new { id = notification.UploadID });
        }
    }
}
public async Task<IActionResult> Preview(int id)
{
    var userId = TempData["UserID"]; // This is always NULL!
    if (userId is null)
    {
        return RedirectToAction("Index", "Home", new { area = "" });
    }
    else
    {
         // do stuff here.
    }
}

 What am I missing???

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
{count} votes

Accepted answer
  1. Anonymous
    2024-02-06T06:01:38.2766667+00:00

    Hi,@Paul Marangoni

    The problem only happens when I load the site via a link within an email. Both TempData and Session don't seem to be available until the site loads

    I tried to reproduce your issue,the cookie(Session cookie/tempdata cookie) might be blocked by samesite policy in your scenairo You could check how you configured TempData cookie in program.cs

    builder.Services.Configure<CookieTempDataProviderOptions>(options =>
    {
        options.Cookie.Name = "TEMPDATA";
        //you have to avoid setting SameSiteMode.Strict here 
        options.Cookie.SameSite = SameSiteMode.Lax;
       
    });
    
    
    

    You could check the cookie in browser: User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. Best regards, Ruikai Feng


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-02-04T17:26:21.51+00:00

    Use the debugger to which return statement is used. Most likely the code is not setting the tempdata.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.