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???

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,456 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,236 questions
{count} votes

Accepted answer
  1. Ruikai Feng - MSFT 2,526 Reputation points Microsoft Vendor
    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) 57,806 Reputation points
    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.