IdentityServer4 仅适用于 HTTPS

IdentityServer4 works only with HTTPS

我正在从 Identity Server 3 迁移到 4。 我在本地开发环境中遇到没有 HTTPS 的 运行 Identity Server 4 问题。 使用 HTTPS - 一切正常。没有它,用户就不会通过身份验证,只会被重定向 到登录页面。未设置 cookie。

我知道 Identity Server 3 曾经有现在没有的 RequireSsl 选项。 我已经在文档中搜索了几个小时,但一无所获。

我使用的是 IdentityServer4 4.1.1 和 AspNet Core 3.1 我的 Startup.cs 看起来像这样:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
                .AddInMemoryClients(Clients.Get())
                .AddInMemoryIdentityResources(Configs.Resources.GetIdentityResources())
                .AddInMemoryApiResources(Configs.Resources.GetApiResources())
                .AddInMemoryApiScopes(Configs.Resources.GetApiScopes())
                .AddTestUsers(Users.Get())
                ..AddDeveloperSigningCredential();

            services.AddControllersWithViews();

            services.AddMvc(options => options.EnableEndpointRouting = false);
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();

            app.UseIdentityServer();
            app.UseAuthorization();

            app.UseEndpoints(endpoints => endpoints.MapControllers());

            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }

我错过了什么?

我想,你在 Chrome 试试吧。当您打开开发控制台 (F12) 时,您很可能会发现 SameSite=None cookie must be secure 的警告。

如果我的上述猜测是正确的,可能有两个原因:您使用自定义的 CookieAuthenticationOptions 并明确设置 options.Cookie.SameSite = SameSiteMode.None(查看您的启动时您没有),或者 default one 不适合你的配置。

您可以像下面这样调整它:

services.AddIdentityServer(options =>
{
    options.Authentication.CookieSameSiteMode = SameSiteMode.Lax;
})

将在本地主机上工作,将阻止托管在您的 IdSrv 根域之外的客户端的静默刷新。因此,您必须选择是将 Lax 用于生产环境,还是仅用于家庭游乐场(但通常 None 不推荐在任何地方使用)。