0

当我在Graph Explorer中进行测试以获取不存在的照片时,我收到了预期的错误...

{
    "error": {
        "code": "ImageNotFound",
        "message": "Exception of type 'Microsoft.Fast.Profile.Core.Exception.ImageNotFoundException' was thrown.",
        "innerError": {
            "date": "2021-11-06T02:23:38",
            "request-id": "4e44e92e-d5b5-4708-8128-01e39dcacc0c",
            "client-request-id": "1786d432-a855-9e1f-a68c-e4f1a7c345d0"
        }
    }
}

但是,当尝试在我的 ASP.Net Core Web 应用程序中使用 MS Graph SDK (4.8.0) 执行相同的请求时,会引发不同的异常......

代码:AadGraphAuthFailed 消息:访问服务失败。

GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(request =>
{
    // Add the access token in the Authorization header of the API request.
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.TokenEndpointResponse.AccessToken);
    return Task.CompletedTask;
}));

try
{
    var user = await graphServiceClient.Me.Request().GetAsync(); // THIS LINE WORKS
    var media = await graphServiceClient.Me.Photo.Request().GetAsync(); // THIS FAILS WITH EXCEPTION AadGraphAuthFailed: Accessing service failed

    if (media != null)
    {
        model.PhotoType = media.AdditionalData["@odata.mediaContentType"].ToString();
        using var photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
        model.Photo = ((MemoryStream)photo).ToArray();
    }

}
catch (ServiceException ex)
{
    var additionalData = ex.Error.AdditionalData;
    if (additionalData != null)
    {
        var details = additionalData["details"];
    }
}

我已经使用 v1.0 和 beta 端点对此进行了测试,并且都因相同的错误而失败。我的 API 权限配置正确并在登录时获得同意。Me 请求也可以正常工作,这表明 Graph SDK 在获取照片时存在问题,但我的代码可能存在问题?

请注意,如果照片确实存在,则不会引发错误

在此处输入图像描述

  "DownstreamApi": {
    /*
     'Scopes' contains space separated scopes of the Web API you want to call. This can be:
      - a scope for a V2 application (for instance api:b3682cc7-8b30-4bd2-aaba-080c6bf0fd31/access_as_user)
      - a scope corresponding to a V1 application (for instance <App ID URI>/.default, where  <App ID URI> is the
        App ID URI of a legacy v1 Web application
      Applications are registered in the https:portal.azure.com portal.
    */
    //"BaseUrl": "https://graph.microsoft.com/v1.0",
    "BaseUrl": "https://graph.microsoft.com/beta",
    "Scopes": "User.Read"
  }

- - 更新 - -

这似乎只影响全局管理员(Microsoft 帐户?)。当我在同一目录中创建新的成员用户帐户时,代码失败并出现正确的 ImageNotFound 异常。因此,Microsoft 帐户有限制吗?

图调用时的调试日志片段...

Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler: Information: AuthenticationScheme: OpenIdConnect was challenged.
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 925.0187ms 302 
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.21\System.Net.WebSockets.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/2.0 POST https://localhost:44308/signin-oidc application/x-www-form-urlencoded 1843
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Users\MyPC\source\repos\MyApp\WebApp\bin\Debug\netcoreapp3.1\Microsoft.IdentityModel.JsonWebTokens.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.21\System.Security.Cryptography.Cng.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.21\Microsoft.Extensions.Identity.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.21\System.Threading.Tasks.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'Microsoft.Graph.ServiceException' in System.Private.CoreLib.dll
Code: AadGraphAuthFailed
Message: Accessing service failed.
Inner error:
    AdditionalData:
    date: 2021-11-10T22:35:18
    request-id: a9becd68-8195-4ba9-9773-f33b21a61ce0
    client-request-id: a9becd68-8195-4ba9-9773-f33b21a61ce0
ClientRequestId: a9becd68-8195-4ba9-9773-f33b21a61ce0

Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler: Information: AuthenticationScheme: Cookies signed in.
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 18195.77ms 302 
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/2.0 GET https://localhost:44308/  
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService: Information: Authorization was successful.
4

0 回答 0