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

问题描述 投票:0回答:1

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

public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IConfiguration>(Configuration);
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath = "/login";
                ....

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

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

asp.net-core asp.net-core-3.1
1个回答
0
投票

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

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

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添加一个using到startup.cs中。

© www.soinside.com 2019 - 2024. All rights reserved.