简体   繁体   English

ASP.NET Core Razor 在使用 Apache 作为反向代理时无法设置 cookie

[英]ASP.NET Core Razor unable to set cookies when using Apache as reverse proxy

I am hosting my ASP.NET Core Razor application on CentOS using Apache as a reverse proxy.我使用 Apache 作为反向代理在 CentOS 上托管我的 ASP.NET Core Razor 应用程序。 I am able to visit the website using my domain however the application is unable to set cookies in my browser no matter what I do.我可以使用我的域访问该网站,但是无论我做什么,该应用程序都无法在我的浏览器中设置 cookie。

This is the configuration for headers in my startup.cs:这是我的 startup.cs 中标头的配置:

services.Configure<ForwardedHeadersOptions>(options =>
        {
            options.KnownProxies.Add(IPAddress.Parse("x.x.x.x"));
            options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        });

app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });

        app.UseAuthentication();

And this is virtual host configuration in apache httpd.conf file:这是 apache httpd.conf 文件中的虚拟主机配置:

  <VirtualHost *:80>
ServerName mysite.com
ServerAlias subdomain.mysite.com
ServerAdmin webmaster@mysite.com

ProxyPreserveHost On
ProxyPass / http://localhost:5001/ retry=0
ProxyPassReverse / http://localhost:5001/ retry=0
</VirtualHost>

I use this code for setting cookies and is not working:我使用此代码设置 cookie 并且不起作用:

 public IActionResult OnPost()
        {
Response.Cookies.Append("mycookie", "cookie-value");
                            return RedirectToPage("/Index");
}

I am not sure if I am doing everything correctly, perhaps I am going wrong somewhere but the fact is my application works fine on localhost and is able set cookies as well.我不确定我做的一切是否正确,也许我在某个地方出错了,但事实是我的应用程序在 localhost 上运行良好,并且也可以设置 cookie。

Ok I found the answer and I can't believe it was that simple.好的,我找到了答案,简直不敢相信这么简单。 In my Startup.cs file I had set cookie policy:在我的 Startup.cs 文件中,我设置了 cookie 策略:

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

Because during the development mode I was consistently using the application and in the initial stages I had given permission for setting cookie but soon after that I removed code need to set the permission true by user as it was the development stage.因为在开发模式期间,我一直在使用该应用程序,并且在初始阶段我已授予设置 cookie 的权限,但不久之后我删除了代码,因为它是开发阶段,因此需要用户将权限设置为 true。

I just changed above into this:我只是把上面改成了这样:

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;
        });

And now cookies are being set without any issue.现在 cookie 的设置没有任何问题。

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

相关问题 具有Apache和反向代理的Linux ASP.Net核心 - Linux ASP.Net core with Apache and reverse proxy How to set BaseUrl for Swagger in ASP.NET Core Web API using NSwag.AspNetCore (when hosted behind a reverse proxy) - How to set BaseUrl for Swagger in ASP.NET Core Web API using NSwag.AspNetCore (when hosted behind a reverse proxy) 无法使用 ASP.NET 核心中的 FluentEmail 发送 Razor 模板 - Unable to send a Razor template using FluentEmail in ASP.NET Core ASP.NET Core YARP 反向代理多个监听端口 - ASP.NET Core YARP Reverse Proxy multiple listen ports Stackify在ASP.NET Core中未使用代理 - Stackify not using proxy in ASP.NET Core Asp.net Core,在 Razor 页面中设置多个选定值 - Asp.net Core, set multiple selected values in Razor Page 如何在认证/授权期间在 asp.net 内核中设置 cookies - How to set cookies in asp.net core during authenticaiton/authorization 使用ASP.NET的简单反向代理,带有身份验证的C# - A simple reverse proxy using ASP.NET, C# with authentication Asp.Net Core 3.1 Cookies 未附加 Razor 页 C# - Asp.Net Core 3.1 Cookies not appended Razor Pages C# 使用 Razor Pages 在 Asp.NET Core 2.2 中找不到 ViewComponent - ViewComponent not found in Asp.NET Core 2.2 using Razor Pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM