简体   繁体   English

在 localhost 上使用 asp.net core 2.1 身份时,TempData 丢失

[英]TempData lost when using asp.net core 2.1 identity on localhost

Installed new asp.net core 2.1 identity (the one using RCL) to play with it.安装了新的asp.net core 2.1 identity (使用 RCL 的身份)来使用它。 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.更有趣的是,当我将它发布到 Azure 时,它​​可以工作。

My Startup.cs do have config as indicated by ofiical docs:我的Startup.cs确实有官方文档所示的配置:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata https://docs.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 https://github.com/kedzior-io/dotnetpwa/tree/model-status-message-is-empty-on-redirect

Any idea what am I missing?知道我错过了什么吗?

So looks like offical docs are no good:所以看起来官方文档不好:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata https://docs.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谢谢@托尼莫里斯

I'm using aspnet core 2.2, and in my case changing the order as suggested by Artur's answer didn't work.我正在使用 aspnet core 2.2,在我的情况下,按照 Artur 的回答所建议的更改顺序不起作用。

I solved this by following the official documentation , where it says:我按照官方文档解决了这个问题,其中说:

Session state cookies are not essential.会话状态 cookie 不是必需的。 Session state isn't functional when tracking is disabled.禁用跟踪时会话状态不起作用。 The following code makes session cookies essential:以下代码使会话 cookie 必不可少:

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

Having this set solves the issue no matter if it's placed before or after services.AddMvc()无论是放在services.AddMvc()之前还是之后,拥有这个集合都可以解决问题

My site's cookie policy meant the TempData cookie was not being saved.我网站的 cookie 政策意味着没有保存 TempData cookie。 So I added code to mark the cookie as "essential":所以我添加了代码来将 cookie 标记为“必需”:

services.AddControllersWithViews()
    .AddCookieTempDataProvider(t => t.Cookie.IsEssential = true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM