【问题标题】:Property Proxy not supported. Blazor Client Side MS Graph API不支持属性代理。 Blazor 客户端 MS 图形 API
【发布时间】:2020-06-23 17:53:38
【问题描述】:

我正在尝试根据在 Blazor 客户端应用程序中检索到的访问令牌从 Microsoft 的 Graph API 检索用户组。

Index.razor.cs 部分。

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using System;
using System.Threading.Tasks;
using Microsoft.Graph;
using System.Net.Http.Headers;
using System.Linq;

namespace blazorapp.Pages
{
    public partial class Index
    {
        [Inject]
        IAccessTokenProvider TokenProvider { get; set; }
        protected override async Task OnInitializedAsync()
        {
            var tokenResult = await TokenProvider.RequestAccessToken(
            new AccessTokenRequestOptions
            {
                Scopes = new[] { "https://graph.microsoft.com/GroupMember.Read.All" }
            });

            if (tokenResult.TryGetToken(out var token))
            {
                Console.WriteLine(token.Value);
                try
                {
                    var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
                    {
                        requestMessage
                            .Headers
                            .Authorization = new AuthenticationHeaderValue("Bearer", token.Value);
                        return Task.FromResult(0);
                    }));
                    var securityEnabledOnly = true;
                    var groupsRequest = await graphServiceClient
                        .Me
                        .GetMemberGroups(securityEnabledOnly)
                        .Request()
                        .PostAsync();
                } catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }
}

它可以很好地返回令牌,并且当在 jwt.ms 或 postman 中使用时,它会很好地返回。但是在浏览器中我得到了

“不支持属性代理。”从我的 catch 块内的控制台日志中。我的代码中是否存在可能导致此问题的明显错误?

【问题讨论】:

  • 能否分享一下你使用代理的代码sn-p

标签: c# azure-active-directory microsoft-graph-api blazor


【解决方案1】:

当您创建 GraphServiceClient 时,客户端正在使用 GraphClientFactory 构造一个针对 Graph API 优化的 HttpClient。然后工厂检查并使用 blazor HttpClient 不支持的一堆属性(如代理)。

您需要提供自己的客户来解决此问题。

var graphHttpClient = new HttpClient(); // example, get your own client properly
var authenticationProvider = new DelegateAuthenticationProvider((requestMessage) => 
{
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.Value);
    return Task.FromResult(0);
});

var graphServiceClient = new GraphServiceClient(graphHttpClient);
graphServiceClient.AuthenticationProvider = authenticationProvider;

【讨论】:

  • 感谢您,我一直在努力寻找有关如何将 GraphServiceClient 与 Blazor WASM 一起使用的任何文档!
【解决方案2】:

这是可能的。您可以执行以下操作:

var serviceGraph = new GraphServiceClient(injectedHttpClient);
serviceGraph.AuthenticationProvider = new DelegateAuthenticationProvider(request =>
{
   request.Headers.Authorization = new AuthenticationHeaderValue("bearer", token.Value);
   return Task.FromResult(0);
});

var result = await serviceGraph.Me.Request().GetAsync();

GraphServiceClient 中有一个接受 HttpClient 的重载构造函数,这是 Blazor WebAssembly 用来向 Web API 发出请求的构造函数。然后使用该对象,您可以像 Peter 演示的那样设置 AuthenticationProvider。

我正在使用 Microsoft Graph v3.8.0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2011-01-13
    • 2020-02-23
    相关资源
    最近更新 更多