You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core Razor Pages部署Somee后Google登录关联失败问题

解决ASP.NET Core Razor Pages部署到Somee后Google登录的"correlation failed"错误

针对你遇到的问题,核心原因是Somee作为反向代理主机,导致应用无法正确识别真实请求的协议、主机信息,加上Cookie配置不匹配,最终触发correlation验证失败。按以下步骤调整配置:

1. 配置反向代理转发头

Somee会修改请求的协议和主机信息,需让应用识别真实外部请求地址:

在Startup.cs的ConfigureServices中添加:

services.AddForwardedHeaders(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    // 清除默认限制,适配Somee代理环境
    options.KnownNetworks.Clear();
    options.KnownProxies.Clear();
});

然后在Configure方法里,放在UseAuthentication之前添加:

app.UseForwardedHeaders();

2. 调整Cookie认证配置

修改Cookie属性,确保在反向代理环境下能正确传递:

替换Startup.cs中的AddCookie部分:

.AddCookie(options =>
{
    options.Cookie.HttpOnly = true;
    options.Cookie.SameSite = SameSiteMode.Lax;
    // 若Somee强制HTTPS,设为Always;否则用SameAsRequest
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
    // 支持www和非www域名的Cookie共享
    options.Cookie.Domain = ".fbittut.somee.com";
})

同时给Google认证显式指定回调路径:

.AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
{
    options.ClientId = Configuration["Authentication:Google:ClientId"];
    options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
    options.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
    options.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
    options.CallbackPath = "/signin-google";
})

3. 更新Google控制台的重定向URI

Somee免费主机通常会自动将HTTP请求重定向到HTTPS,需在Google Cloud Console中添加HTTPS版本的重定向URI:

  • https://www.fbittut.somee.com/signin-google
  • https://fbittut.somee.com/signin-google

4. 修正Challenge的RedirectUri

确保跳转地址是真实外部地址,避免相对路径问题:

public class GoogleModel : PageModel
{
    public IActionResult OnGet() 
    {
        var redirectUri = Url.Page("/LoginPage/Login", pageHandler: null, values: null, protocol: Request.Scheme);
        return Challenge(new AuthenticationProperties { RedirectUri = redirectUri }, "Google");
    }
}

5. 显式配置Correlation Cookie(可选)

如果上述步骤无效,给Google认证的Correlation Cookie添加适配配置:

.AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
{
    // 其他已有配置...
    options.CorrelationCookie.HttpOnly = true;
    options.CorrelationCookie.SameSite = SameSiteMode.Lax;
    options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
    options.CorrelationCookie.Domain = ".fbittut.somee.com";
})

内容的提问来源于stack exchange,提问作者Quốc Vô

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 03:44:52