ASP.NET 核心OAuth不适用于Safari



我们最近发现OAuth(包括facebook/google(的某些内容在桌面Safari(12.x和13.x(上不起作用。该系统适用于Chrome,Firefox和移动Safari。

我们不确定它什么时候坏了,但它在 2 周前工作,昨晚没有工作。

以下是我们在启动中配置 OAuth 的地方.cs

.AddFacebook(options => {
options.AppId = SecretSettings.GetSecret("FacebookAppId");
options.AppSecret = SecretSettings.GetSecret("FacebookAppSecret");
options.Scope.Add("email");
options.AuthorizationEndpoint = "https://www.facebook.com/v2.8/dialog/oauth";
options.TokenEndpoint = "https://graph.facebook.com/v2.8/oauth/access_token";
options.BackchannelHttpHandler = new FacebookBackChannelHandler();
options.UserInformationEndpoint = "https://graph.facebook.com/v2.8/me?fields=id,name,email,first_name,last_name";
options.RemoteAuthenticationTimeout = remoteAuthTimeout;
options.SignInScheme = IdentityConstants.ExternalScheme;
options.AccessDeniedPath = "/account/login?returnUrl=%2F";
})
.AddGoogle(options => {
options.ClientId = SecretSettings.GetSecret("GoogleOAuth2ClientId");
options.ClientSecret = SecretSettings.GetSecret("GoogleOAuth2ClientSecret");
options.RemoteAuthenticationTimeout = remoteAuthTimeout;
options.SignInScheme = IdentityConstants.ExternalScheme;
options.AccessDeniedPath = "/account/login?returnUrl=%2F";
})

我们使用以下代码生成挑战:

return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl }, authenticationScheme);

据我们所知,通常的流程是:

  1. 点击谷歌登录,生成挑战令牌
  2. 重定向至谷歌oauth,用户登录并返回
  3. 重定向至/signin-google(中间件路由(以确认给定的代码
  4. 重定向至sso返回操作,处理用户登录或注册

Chrome 正确击中了所有这些,但 Safari 卡在 3 上。浏览器不会被重定向回我们的 sso 返回操作,而是被丢弃在网站的主页上。

我检查了所涉及的网址,看起来状态、范围和代码被正确传递,只是中间件内部发生了一些事情,将用户踢到站点的根目录。

我们正在使用 ASP.NET 酷睿3.0

有没有人知道可能出现什么问题?或者我们可以探索的途径来弄清楚?

我可能是错的,但这是否与ITP(智能跟踪预防(和尝试进行重定向时缺少的用户手势有关? https://webkit.org/blog/9521/intelligent-tracking-prevention-2-3/

这类事情通常是由供应商引入的,对我们这些使用现代应用程序安全标准的人来说很少考虑。

我遇到了同样的问题。 我使用此处找到的解决方案修复了它:https://github.com/dotnet/aspnetcore/issues/18362

在 Startup.cs 中添加 SameSite cookie 建议的代码:

services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
options.OnAppendCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
options.OnDeleteCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
});

以及以下方法:

private void CheckSameSite(HttpContext httpContext, CookieOptions options)
{
if (options.SameSite == SameSiteMode.None)
{
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
if (DisallowsSameSiteNone(userAgent))
{
options.SameSite = SameSiteMode.Unspecified;
}
}
}
public static bool DisallowsSameSiteNone(string userAgent)
{
// Cover all iOS based browsers here. This includes:
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
// All of which are broken by SameSite=None, because they use the iOS networking
// stack.
if (userAgent.Contains("CPU iPhone OS 12") ||
userAgent.Contains("iPad; CPU OS 12"))
{
return true;
}
// Cover Mac OS X based browsers that use the Mac OS networking stack. 
// This includes:
// - Safari on Mac OS X.
// This does not include:
// - Chrome on Mac OS X
// Because they do not use the Mac OS networking stack.
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
userAgent.Contains("Version/") && userAgent.Contains("Safari"))
{
return true;
}
// Cover Chrome 50-69, because some versions are broken by SameSite=None, 
// and none in this range require it.
// Note: this covers some pre-Chromium Edge versions, 
// but pre-Chromium Edge does not require SameSite=None.
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
{
return true;
}
return false;
}

这将禁止将同一站点策略发送到与其不兼容的浏览器。

将以下代码添加到您的Startup.cs

services.ConfigureApplicationCookie(options => options.Cookie.SameSite = SameSiteMode.None);

此解决方案基于线程 https://github.com/dotnet/aspnetcore/issues/4647。

最新更新