首页 > 解决方案 > 对于涉及跨域请求的 SSO 流,在 iOS 12 中阻止了实施 SameSite 策略的 Cookie

问题描述

摘要:iOS / OS 12 中的第三方登录中断!

我们有一个跨多个网站的通用登录。这在 Windows、macOS 和 iOS 上的 Firefox、Chrome 和 Safari 中运行良好。但是在 iOS 12 和 macOS 12 中,cookie 似乎不再从 auth0 登录窗口到我们的登录 API 工作。

它不仅在 Safari 中停止工作,而且在 iOS 12 上也在 Chrome 和 Firefox 中停止工作(它仍然在 Mac OS 12 上的 Chrome 中工作)。我怀疑这与智能跟踪预防 2.0 有关,但我很难找到许多技术细节。

我们的登录流程如下:

  1. 用户单击登录,它将 window.location.href 设置为通用(不同)登录域上的登录 url。
  2. 这会调用 ChallengeAsync,它将用户发送到 auth0 域进行登录。
  3. 然后将用户发送回登录域,但此时来自 auth0 的 cookie 和控制器中设置的会话 cookie 丢失了。

我在启动时使用以下内容:

    services.AddAuthentication(options => {
                    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                })
                .AddCookie(options =>
                {
                    options.Cookie.Path = "/";
                    options.SlidingExpiration = false;
                })
                .AddOpenIdConnect("Auth0", options => {
                // Set the authority to your Auth0 domain
                options.Authority = $"https://{Configuration["Auth0:Domain"]}";

                // Configure the Auth0 Client ID and Client Secret
                options.ClientId = Configuration["Auth0:ClientId"];
                options.ClientSecret = Configuration["Auth0:ClientSecret"];

                // Set response type to code
                options.ResponseType = "code";

                // Configure the scope
                options.Scope.Clear();
                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.Scope.Add("email");
                options.Scope.Add("offline_access");
                options.CallbackPath = new PathString("/signin-auth0");
                options.ClaimsIssuer = "Auth0";
                options.SaveTokens = true;
                options.Events = new OpenIdConnectEvents
                {
                    OnRemoteFailure = context => {
                        <not relevant error redirects>    
                    },
                    OnRedirectToIdentityProvider = context =>
                    {
                        context.ProtocolMessage.SetParameter("audience", $"{ Configuration["Auth0:ApiIdentifier"]}");    
                        return Task.FromResult(0);
                    },
                    OnRedirectToIdentityProviderForSignOut = (context) =>
                    {
                        <not relevant logout handling>
                    }
                };
            });

在登录控制器中,我有一个登录操作,它只设置一个会话值并调用 ChallengeAsync 来打开 Auth0 登录:

await HttpContext.ChallengeAsync("Auth0", new AuthenticationProperties() { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddMinutes(Global.MAX_LOGIN_DURATION_MINUTES), RedirectUri = returnUri });

“returnUri”参数是返回同一控制器的完整路径,但操作不同。正是当此操作被触发时,来自 auth0 登录的两个 cookie(即https://ourcompany.eu.auth0.com)和我在登录操作中设置的会话数据在 iOS 12 中都丢失了。

我可以通过其他适用于 iOS 12 的方式进行操作吗?所有帮助表示赞赏。

标签: c#.net-coreauth0ios12

解决方案


我终于想通了。默认设置的 cookie 使用SameSiteMode.Lax. 在 iOS 12 之前,这在任何地方都可以正常工作,现在需要将其设置为SameSiteMode.None.

这是我使用的修改让它再次工作:

.AddCookie(options =>
            {
                options.Cookie.Path = "/";
                options.SlidingExpiration = false;
                options.Cookie.SameSite = SameSiteMode.None;
                options.Cookie.Expiration = TimeSpan.FromMinutes(Global.MAX_LOGIN_DURATION_MINUTES);
            })

推荐阅读