aspnet / Identity

[Archived] ASP.NET Core Identity is the membership system for building ASP.NET Core web applications, including membership, login, and user data. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
1.97k stars 871 forks source link

TempData doesn't work when scaffolded #2016

Closed kedzior-io closed 5 years ago

kedzior-io commented 5 years ago

When using scaffolded asp.net core 2.1 identity:

  1. TempData doesn't work when using cookie provider:

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

  1. TempData doesn't work when using session provider if

services.AddSession(); is placed AFTER services.AddMvc();

Apparently the order doesn't matter.


I have tested it with scaffolded index page of the profile:

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

Updating phone number sets TempData property:

StatusMessage = "Your profile has been updated";

But it's not rendered in the view:

@Html.Partial("_StatusMessage", Model.StatusMessage)

blowdart commented 5 years ago

This is probably because the cookie for the temp data provider isn't marked as essential and you haven't wired up the cookie consent pieces. Please take a look at https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.1 and try integrating that.

blowdart commented 5 years ago

We're closing this issue as no response or updates have been provided in a timely manner. If you have more details and are encountering this issue please add a new reply and re-open the issue.

kedzior-io commented 5 years ago

Yeah, sorry about responsiveness and thanks for your time. Read the document and so what I understand is that to disabled GDPR but still use TempData I do the following:

services.Configure<CookiePolicyOptions>(options => {
                options.CheckConsentNeeded = context => false; // ignore user's consent
                options.MinimumSameSitePolicy = SameSiteMode.None;
});

...

services.Configure<CookieTempDataProviderOptions>(options => {
               options.Cookie.IsEssential = true; // mark it as essential
 });

...

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();         
app.UseAuthentication();
app.UseMvc();

That still won't work.

kedzior-io commented 5 years ago

Ok that seems my bad. The above configuration works. While editing template I didn't add @await Html.PartialAsync("_StatusMessage", Model.StatusMessage) to the particular view I was testing.