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

.NET Blazor MAUI谷歌认证登录后无法重定向回移动端问题

Blazor MAUI谷歌认证回调404问题排查与修复

核心问题定位:Callback Scheme 全链路不匹配

谷歌授权后返回404,基本是回调Scheme的配置在各环节不一致导致的,按以下步骤逐一校验修复:

1. 谷歌开发者控制台配置

  • 在Web应用凭据中,添加回调地址 cflow://callback(Scheme部分严格匹配你指定的cflow,大小写敏感,无多余符号)

2. Web API AuthController代码校验

  • 确认callbackScheme参数和APP端Scheme完全一致:
var properties = new AuthenticationProperties
{
    RedirectUri = "/signin-google",
    Items =
    {
        { "callbackScheme", "cflow" }
    }
};
return Challenge(properties, GoogleDefaults.AuthenticationScheme);

3. Web API Program.cs配置

  • 正确注册谷歌认证并指定回调路径,同时保证中间件顺序:
builder.Services.AddAuthentication()
    .AddGoogle(options =>
    {
        options.ClientId = builder.Configuration["Google:ClientId"];
        options.ClientSecret = builder.Configuration["Google:ClientSecret"];
        options.CallbackPath = "/signin-google";
    });

// 中间件顺序:认证在前,授权在后
app.UseAuthentication();
app.UseAuthorization();

4. Blazor MAUI Android端配置

AndroidManifest.xml

  • 注册Scheme,android:scheme必须和cflow完全一致:
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="cflow" />
</intent-filter>

MainActivity.cs

  • 调用WebAuthenticator时,回调Uri严格匹配Scheme:
var authResult = await WebAuthenticator.AuthenticateAsync(
    new Uri("https://你的API域名/auth/google"),
    new Uri("cflow://callback"));

Xamarin中同类问题解决办法

Xamarin.Forms/Xamarin.Android的排查逻辑和MAUI一致,核心保证Scheme全链路匹配,额外注意:

1. Android端配置

  • 将Scheme的intent-filter放在主Activity的标签内,格式和MAUI一致
  • 确保使用最新版Xamarin.Essentials包,避免旧版本WebAuthenticator的兼容性Bug

2. Xamarin.Forms端代码

  • 调用WebAuthenticator时,回调Uri必须和所有配置一致:
var result = await WebAuthenticator.AuthenticateAsync(
    new Uri("https://你的API域名/auth/google"),
    new Uri("cflow://callback"));

3. 额外处理:重写OnNewIntent方法

部分设备需在MainActivity中手动处理回调:

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    Xamarin.Essentials.Platform.OnNewIntent(intent);
}

内容的提问来源于stack exchange,提问作者maztt

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 13:15:32