BlazorMaui中使用Google登录无法成功
创始人
2024-12-21 12:00:03
0

Blazor Maui 中使用 Google 登录的步骤与 Blazor WebAssembly 有些不同,需要额外的配置和代码。

首先,需要在 Google 开发者控制台中创建一个 OAuth 客户端 ID,并将“已授权的 JavaScript 源”字段设置为“localhost:8080”。然后,将以下代码添加到项目的 Program.cs 文件中:

using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.AspNetCore.Components.WebView.Maui;
using Microsoft.Extensions.DependencyInjection;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

namespace MyAppNamespace
{
    public class Program
    {
        ...
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
              .UseMauiApp()
              .ConfigureServices(services =>
              {
                  // 添加 Google 登录服务
                  services.AddGoogleAuthentication(Configuration);
              })
              .ConfigureMauiHandlers(handlers =>
              {
                  handlers.AddHandler();
              });

            return builder.Build();
        }
    }
} 

其中,services.AddGoogleAuthentication(Configuration) 会添加 Google 登录服务,并需要在 appsettings.json 文件中添加以下配置:

{
  "Google": {
    "ClientId": "your_client_id_here.apps.googleusercontent.com",
    "ClientSecret": "your_client_secret_here"
  }
}

最后,在组件中使用以下代码实现登录:

@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

@inject NavigationManager Navigation
@inject SignOutSessionStateManager SignOutManager
@using MyAppNamespace.Auth
...

    
        
            
/ Welcome, @context.User.FindFirst(c => c.Type == "name")?.Value!
@{ var returnUrl = Navigation.ToBaseRelativePath(Navigation.Uri); var loginUrl = $"{Navigation.BaseUri}authentication/login?returnUrl={returnUrl}"; } <

相关内容

热门资讯

透视指引!werplan透视挂... 透视指引!werplan透视挂(透视)总是真的有辅助软件(哔哩哔哩)1、完成werplan透视挂辅助...
透视经验!htx矩阵wepok... 透视经验!htx矩阵wepoker辅助(透视)确实存在有辅助插件(哔哩哔哩)1、htx矩阵wepok...
透视讲义!hhpoker德州挂... 透视讲义!hhpoker德州挂真的有吗(透视)好像是真的辅助插件(哔哩哔哩)1)hhpoker德州挂...
透视指南!拱趴大菠萝自动计算机... 透视指南!拱趴大菠萝自动计算机器人(透视)确实存在有辅助神器(哔哩哔哩)1、下载好拱趴大菠萝自动计算...
透视妙招!佛手在线大菠萝为什么... 透视妙招!佛手在线大菠萝为什么都输(透视)确实有辅助工具(哔哩哔哩)1)佛手在线大菠萝为什么都输辅助...
透视秘籍!pokerworld... 透视秘籍!pokerworld软件(透视)确实是真的辅助app(哔哩哔哩)小薇(辅助器软件下载)致您...
透视教材!德州之星扫描器(透视... 透视教材!德州之星扫描器(透视)好像是真的辅助插件(哔哩哔哩)1、任何德州之星扫描器透视是真的假的的...
透视模块!wepoker怎么拿... 透视模块!wepoker怎么拿到好牌(透视)好像有辅助修改器(哔哩哔哩)1、下载好wepoker怎么...
透视诀窍!德普之星私人局辅助免... 透视诀窍!德普之星私人局辅助免费(透视)一贯存在有辅助插件(哔哩哔哩)所有人都在同一条线上,像星星一...
透视学习!wepoker手机版... 透视学习!wepoker手机版透视脚本(透视)好像是真的辅助工具(哔哩哔哩)1、每一步都需要思考,不...