首页 > 解决方案 > 尽管两个身份验证都针对本地身份服务器 4,但两个本地主机站点不共享 cookie

问题描述

我在使用 Identity server 4 进行单点登录时遇到问题。

当我关注 Identity server 4 (with aspnet core 2) 视频和文章时,我已经开发了一个示例应用程序 + 本地身份服务器。我有:

除了客户端 ID 外,这两个应用程序都是相同的 Asp.net core 2 (MVC)。他们具有相同的权限https://localhost:5001

现在,当我尝试针对身份服务器对应用程序 1 进行身份验证时,它给了我登录页面、验证用户(针对 aspnet 身份)并将主页返回给我。我可以看到索赔也按预期进行。

应用程序 2 和身份服务器也是如此。

但是现在的问题是:如果我完成了应用程序 1 的身份验证并尝试刷新应用程序 2 或身份服务器上的页面,则没有任何身份验证。如果我尝试访问安全页面,它会给我来自身份服务器 4 的登录屏幕,就像没有进行身份验证一样。同样,我尝试通过直接在 Identity Server 4 上进行身份验证并刷新应用程序(1 和 2)来进行检查,但没有运气。

好像我错过了什么。这是我的配置:

应用程序方面我有:

options.Authority = authenticationSettings.IdentityServer.Authority;
        options.RequireHttpsMetadata = bool.Parse(authenticationSettings.IdentityServer.RequireHttpsMetadata);
        options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

        options.ClientId = authenticationSettings.IdentityServer.ClientId;
        options.GetClaimsFromUserInfoEndpoint = true;
        options.ResponseType = "code id_token";
        options.SaveTokens = true;
        options.ClientSecret = "secret";

        options.Scope.Add("openid");
        options.Scope.Add("profile");
        options.Scope.Add("email");

在 Startup 类中,ConfigureServices 的代码在这里...

public void ConfigureServices(IServiceCollection services)
    {
        AuthenticationSettings authenticationSettings = services.RegisterAuthenticationSettings(configuration: Configuration);

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddCookie()
       .AddOpenIdConnect(options => SetOpenIdConnectOptions(options, authenticationSettings));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

对于配置方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {

        app.UseCookiePolicy();
        app.UseAuthentication();
    }

身份服务器端我有以下客户端(与应用程序 1 和 2 相同,但客户端 ID 除外):

 new Client
            {
                ClientId = "mvc01",
                ClientName = "MVC 01 Demo",
                AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
                ClientSecrets = { new Secret("secret".Sha256()) },
                AllowAccessTokensViaBrowser = true,
                RequireConsent = false,
                RedirectUris =           { "https://localhost:44317/signin-oidc"},
                PostLogoutRedirectUris = { "https://localhost:44317/signout-callback-oidc" },
                BackChannelLogoutUri = "https://localhost:44317/signout-callback-oidc",
                FrontChannelLogoutUri  = "https://localhost:44317/signout-callback-oidc",
                AllowedCorsOrigins =     { "https://localhost:44317" },
                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.Email,
                    "office"
                }
            }

我还尝试检查 chrome 开发人员工具栏下的应用程序信息,发现它们单独存储 cookie(.AspnetCore.Cookies、.AspnetCore.Cookies1、.AspnetCore.Cookies2),但仅适用于该站点)如果我去其他应用程序(我没有登录的地方)我在那里变得空白。

有什么建议吗?

在安东建议之后,这是行为: 在此处输入图像描述

在此处输入图像描述

标签: asp.net-coreidentityserver4openid-connect

解决方案


推荐阅读