4

I have a site I'm playing with to get the hang of Razor Pages. I have a weird situation I'm unsure what is happening or how to resolve. I'm using [TempData] to pass a message on redirect. The app works perfectly locally. Once published to Azure I add a new item and the item is added, I'm redirected to the index page but I never see the TempData message.

Here is my Index page:

public class IndexModel : PageModel
{
    private readonly TheFishRoom_MVC_Core.Data.FishRoomDbContext _context;

    public IndexModel(TheFishRoom_MVC_Core.Data.FishRoomDbContext context)
    {
        _context = context;
    }

    public IList<Coral> Coral { get; set; }

    [TempData]
    public string Message { get; set; }

    public bool ShowMessage => !string.IsNullOrEmpty(Message);

    public async Task OnGetAsync(string searchString)
    {
        if (!String.IsNullOrEmpty(searchString))
        {
            Coral = await _context.Corals.Where(s => s.Name.Contains(searchString)).ToListAsync();
        }
        else
        {
            Coral = await _context.Corals.ToListAsync();
        }
    }
}
} 

Here is my Create page:

namespace TheFishRoom_MVC_Core.Pages.Corals
{
[Authorize(Roles = "Admin")]
public class CreateModel : PageModel
{
    private readonly FishRoomDbContext _context;

    public CreateModel(FishRoomDbContext context)
    {
        _context = context;
    }

    public IActionResult OnGet()
    {
        return Page();
    }

    [BindProperty]
    public Coral Coral { get; set; }

    [TempData]
    public string Message { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.Corals.Add(Coral);
        await _context.SaveChangesAsync();

        Message = "New Coral created successfully!";

        return RedirectToPage("./Index");
    }
}
}

Locally the site works... but not with published to Azure.

Local result: enter image description here

Any help is appreciated!

Julien-L
  • 5,267
  • 3
  • 34
  • 51
Tim
  • 563
  • 2
  • 6
  • 20
  • Are you hosting Web Farm (two or more instances) in Azure? If you so, you need to use Azure Redis Cache. – Win Jun 14 '18 at 20:14
  • I am not this is a single server instance... – Tim Jun 14 '18 at 20:19
  • 1
    Did you ever find a solution to this? I'm having the exact same problem when publishing to Azure (TempData showing alert on redirect works fine locally, but not on Azure). – user2315985 Aug 14 '18 at 10:28
  • Having the same issue here using .NET Core 2.1. Can't figure out why. – smurtagh Oct 30 '18 at 13:30

1 Answers1

8

Ran into the same issue.

Turned on streaming logs on azure and found the following message:

Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware: Cookie '.AspNetCore.Mvc.CookieTempDataProvider' suppressed due to consent policy

Turns out when I was scaffolding my site I stripped the GDPR Cookie Consent code that comes out of the box in .net Core MVC apps that will create the .AspNet.Consent cookie (with a value of "yes") when accepted.

Once that cookie was created, TempData started working.

You can also update the cookie policy options to not check for consent by setting CheckConsentNeeded to false if you are not subject to GDPR.

 services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => false;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
smurtagh
  • 529
  • 6
  • 17