.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
相关产品推荐
相关产品推荐

