2

Installed new asp.net core 2.1 identity (the one using RCL) to play with it. Scaffolded Login, Registration and Profile pages. Checking profile page:

Areas.Identity.Pages.Account.Manage.Index.cshtml.cs

I came accross this property:

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

which is set when updating profile page:

    StatusMessage = "Your profile has been updated";
    return RedirectToPage();

The funny thing is the message is not shown when running locally. The funnier one is when I publish it to Azure, it works.

My Startup.cs do have config as indicated by ofiical docs:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata

which goes:

 .AddSessionStateTempDataProvider()

and

 app.UseSession();

Here is the full source code:

https://github.com/kedzior-io/dotnetpwa/tree/model-status-message-is-empty-on-redirect

Any idea what am I missing?

Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58
  • Can you provide a link to the official docs you're referencing? Just so I'm on the same page with this. – Tony Morris Oct 05 '18 at 23:04
  • question updated – Artur Kedzior Oct 05 '18 at 23:13
  • 1
    Not sure why yours isn't working, but here's an example of the things you said, and it's working for me locally. https://github.com/afmorris/StackOverflow-52674151. Hope this helps. – Tony Morris Oct 05 '18 at 23:40
  • Thanks. Yep, seems to be working ok. Anyways, here is my source code: https://github.com/kedzior-io/dotnetpwa/tree/model-status-message-is-empty-on-redirect . Looking for differences. – Artur Kedzior Oct 06 '18 at 00:53

3 Answers3

3

So looks like offical docs are no good:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata

Docs:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
        .AddSessionStateTempDataProvider();

    services.AddSession();
}

Should be:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddSession(); // That should be BEFORE .AddMvc()

    services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
        .AddSessionStateTempDataProvider();


}

Thanks @Tony Morris

Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58
2

I'm using aspnet core 2.2, and in my case changing the order as suggested by Artur's answer didn't work.

I solved this by following the official documentation, where it says:

Session state cookies are not essential. Session state isn't functional when tracking is disabled. The following code makes session cookies essential:

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

Having this set solves the issue no matter if it's placed before or after services.AddMvc()

zed
  • 2,298
  • 4
  • 27
  • 44
0

My site's cookie policy meant the TempData cookie was not being saved. So I added code to mark the cookie as "essential":

services.AddControllersWithViews()
    .AddCookieTempDataProvider(t => t.Cookie.IsEssential = true);
NickG
  • 9,315
  • 16
  • 75
  • 115