.NET MAUI Android端Google登录中Web Authenticator的正确用法
.NET MAUI Web Authenticator 回调URL配置问题
我按照.NET MAUI Web Authenticator官方文档配置,但文档缺少回调URL的关键信息。按文档要求使用myapp://作为回调URL时,WebAuthenticatorActivity报错:
You must subclass the WebAuthenticatorCallbackActivity and create an IntentFilter for it which matches your callbackUrl.
我参考了第三方辅助库、相关视频及GitHub示例,其中用myapp作为回调URL,但Uri类报错:
Invalid URI: The format of the URI could not be determined.
我尝试组合配置:Web Authenticator回调URL用myapp://,WebAuthenticatorActivity用myapp,虽能运行但Google控制台不允许将myapp://或myapp设为重定向URL。改用https://mysite/signin-google作为Google控制台重定向URL后,登录流程在Google从浏览器重定向回应用时失败,报错:
This mysite page can't by found. No webpage was found for the web address: https://mysite/signin-google
现需明确以下三个配置项的正确取值:
- Web Authenticator的回调URL
- WebAuthenticatorActivity的配置值
- Google控制台的重定向URL
文件 MauiProgram.cs
using CommunityToolkit.Maui; using FertilizerFarm.Helpers; using FertilizerFarm.Services; using FertilizerFarm.View; using Refit; using Telerik.Maui.Controls.Compatibility; namespace FertilizerFarm; public static class Constants { public const string BaseUrl = "https://mysite/mobileauth/"; public const string CallbackScheme = "myapp://"; } public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseTelerik() .UseMauiApp<App>() .UseMauiCommunityToolkit() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); fonts.AddFont("telerikfontexamples.ttf", "TelerikFontExamples"); }); builder.Services.AddSingleton<IConnectivity>(Connectivity.Current) .AddSingleton(WebAuthenticator.Default) .AddSingleton(SecureStorage.Default) .AddRefitClient<IUserProfileService>() .AddSingleton<AuthService>() .AddSingleton<JWTService>() .AddTransient<LoginViewModel>() .AddTransient<Login>(); return builder.Build(); } public static IServiceCollection AddRefitClient<T>(this IServiceCollection services) where T : class { services.AddSingleton(sp => { var settings = new RefitSettings { AuthorizationHeaderValueGetter = () => sp.GetRequiredService<ISecureStorage>().GetAsync("access_token") }; return RestService.For<T>(Constants.BaseUrl, settings); }); return services; } }
文件 WebAuthenticatorCallbackActivity.cs
using Android.App; using Android.Content; using Android.Content.PM; namespace FertilizerFarm; [Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)] [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = CALLBACK_SCHEME)] public class WebAuthenticatorActivity : Microsoft.Maui.Authentication.WebAuthenticatorCallbackActivity { const string CALLBACK_SCHEME = "myapp"; }
文件 LoginViewModel.cs
[RelayCommand] async Task SocialLogin() { try { var result = await _webAuthenticator.AuthenticateAsync(new WebAuthenticatorOptions { CallbackUrl = new Uri($"{Constants.CallbackScheme}"), Url = new Uri(Constants.BaseUrl + "google") }); await _storage.SetAsync("access_token", result.AccessToken); using var response = await _userProfile.GetProfileClaims(); if (response.IsSuccessStatusCode) { Claims.Clear(); var claims = response.Content.Select(x => $"{x.Key}: {x.Value}"); foreach (var claim in claims) Claims.Add(claim); } } catch (Exception ex) { } }
内容的提问来源于stack exchange,提问作者Pretorius
相关产品推荐
相关产品推荐

