[英]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.