Asp.Net Core Cookie身份验证选项的登录路径通过HTTP路由

时间:2020-05-14 22:11:05

标签: asp.net-core asp.net-core-3.1

startup.cs的以下部分显示我正在使用基于cookie的身份验证。当未经身份验证的用户尝试访问受保护的资源时,将使用“ LoginPath”选项。问题是这是通过HTTP完成的。我希望结果响应/重定向到登录页面为HTTPS。

import subprocess
subprocess.call(["fullPath\\yourExe.exe"])

我尝试对LoginPath进行硬编码,以便将其强制通过HTTPS路径,但是我发现该选项必须是相对路径。

有一个下游进程(服务器/负载均衡器/某物),我无权或无权执行重定向到HTTPS的操作,但这不是在HTTP响应发生之前。我不希望该下游进程必须处理HTTP请求。我希望这是在应用程序中处理的。

1 个答案:

答案 0 :(得分:0)

我以前查看并掩盖了以下答案,但是它似乎可以满足我的需要:ASP.NET Core CookieAuthenticationOptions.LoginPath on different domain

在startup.cs的配置服务中,我最终的,经过修改的(删除了几个选项)解决方案:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Login";
                    options.Events = new CookieAuthenticationEvents()
                    {
                        OnRedirectToLogin = (context) =>
                        {
                            context.HttpContext.Response.Redirect(context.RedirectUri.Replace("http://", "https://"));
                            return Task.CompletedTask;
                        }
                    };
                });

如果尚未为System.Threading.Tasks添加一个用户名,请使用它。

相关问题