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

升级Microsoft.Graph至5.0后Blazor WebAssembly GraphClientExtensions报错求助

解决Microsoft Graph 5.x升级后Blazor WASM中IAuthenticationProvider等类型找不到的问题

问题原因

Microsoft Graph 5.0.0及Microsoft.Graph.Core 3.0.0对核心API进行了重大重构,移除了旧版中的IAuthenticationProvider、IHttpProvider、ISerializer接口,改用基于Kiota的全新架构,因此旧代码中的这些类型会无法找到。

解决方案

重构GraphClientExtensions代码,适配Graph 5.x的新API,结合Blazor WebAssembly的认证机制实现Graph客户端的注入:

修改后的完整代码

using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.Graph;
using Microsoft.Kiota.Abstractions.Authentication;

internal static class GraphClientExtensions
{
    public static IServiceCollection AddGraphClient(
        this IServiceCollection services, params string[] scopes)
    {
        // 配置MSAL需要额外同意的权限范围
        services.Configure<RemoteAuthenticationOptions<MsalProviderOptions>>(
            options =>
            {
                foreach (var scope in scopes)
                {
                    options.ProviderOptions.AdditionalScopesToConsent.Add(scope);
                }
            });

        // 注册GraphServiceClient,适配Blazor WASM认证
        services.AddScoped<GraphServiceClient>(sp =>
        {
            var tokenProvider = sp.GetRequiredService<IAccessTokenProvider>();
            var httpClient = sp.GetRequiredService<HttpClient>();

            // 实现Kiota认证提供器,适配Blazor的令牌服务
            var authProvider = new BaseBearerTokenAuthenticationProvider(
                new BlazorTokenProvider(tokenProvider));

            // 使用HttpClientAdapter将自定义认证与HttpClient绑定
            var adapter = new HttpClientAdapter(httpClient, authProvider);
            
            return new GraphServiceClient(adapter);
        });

        return services;
    }

    // 适配Kiota的ITokenProvider接口,对接Blazor的IAccessTokenProvider
    private class BlazorTokenProvider : ITokenProvider
    {
        private readonly IAccessTokenProvider _blazorTokenProvider;

        public BlazorTokenProvider(IAccessTokenProvider tokenProvider)
        {
            _blazorTokenProvider = tokenProvider;
        }

        public async Task<AccessTokenResult> GetAuthorizationTokenAsync(
            Uri uri, Dictionary<string, object> additionalAuthenticationContext = null,
            CancellationToken cancellationToken = default)
        {
            var tokenRequestResult = await _blazorTokenProvider.RequestAccessToken(
                new AccessTokenRequestOptions
                {
                    Scopes = new[] { "https://graph.microsoft.com/User.Read" }
                });

            if (tokenRequestResult.TryGetToken(out var token))
            {
                return new AccessTokenResult(
                    AccessTokenResultStatus.Success, 
                    token.Value, 
                    DateTimeOffset.UtcNow.AddSeconds(token.ExpiresIn));
            }

            return new AccessTokenResult(AccessTokenResultStatus.Failure, null, null);
        }
    }
}

关键改动说明

  1. 认证模型替换:

    • 移除旧的IAuthenticationProvider,改用Kiota提供的BaseBearerTokenAuthenticationProvider和ITokenProvider接口
    • 自定义BlazorTokenProvider类实现ITokenProvider,对接Blazor的IAccessTokenProvider获取令牌
  2. HTTP处理简化:

    • 不再需要自定义IHttpProvider,Graph 5.x通过HttpClientAdapter直接使用DI容器中注册的HttpClient
  3. 依赖注入优化:

    • 直接在AddScoped中构造GraphServiceClient,无需单独注册多个中间依赖类型

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 14:45:04