尝试生成 Microsoft Graph 令牌时出现 Microsoft.Identity.Client.MsalUiRequiredException 错误

问题描述 投票:0回答:1

嗨,团队,

我在尝试使用下面的代码生成令牌时遇到以下错误

Microsoft.Identity.Client.MsalUiRequiredException:'AADSTS50076:由于管理员进行的配置更改,或者由于您移动到新位置,您必须使用多重身份验证才能访问'00000003-0000-0000-c000-000000000000 '。跟踪 ID:43e876a2-7f8d-4631-9b4d-8c8c875c8900 相关 ID:ab584317-4e37-41ce-96c1-8610f6bd5293 时间戳:2024-05-08 09:40:05Z'

尝试 { 结果=等待app.AcquireTokenByUsernamePassword( 范围:范围, 用户名:this._loginId, 密码:this._loginPassword ).ExecuteAsync(); }

错误截图: 在此输入图片描述

我想做什么? 我正在尝试使用 User Id 和 Password 获取令牌,我们将使用该令牌来调用 Graph 对象 baseUri=https://graph.microsoft.com/beta

your text

在此输入图片描述

azure microsoft-graph-api
1个回答
0
投票

如本MS 文档中所述,

如果用户需要使用多重身份验证(MFA)来登录应用程序,则在使用用户名密码流程生成令牌时,他们将被阻止

最初。当我尝试使用用户名密码流为启用了 MFA 的用户生成令牌时,我也遇到了同样的错误

enter image description here

要解决该错误,请切换到交互式流程以生成访问令牌,该令牌涉及用户至少登录一次以完成 MFA。

使用交互式流程时,请确保将重定向 URI 添加为 http://localhost 作为移动和桌面应用程序平台中的重定向 URI:

enter image description here

当我运行下面的代码以交互流生成令牌时,它要求我使用 MFA 登录并成功获得包含团队应用程序列表的响应,如下所示:

using System.Net.Http.Headers;
using Microsoft.Identity.Client;
using System.Text.Json;

class Program
{
    private static string clientId = "appId";
    private static string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
    private static string redirectUri = "http://localhost";

    static async Task Main(string[] args)
    {
        var app = PublicClientApplicationBuilder.Create(clientId)
            .WithAuthority(AzureCloudInstance.AzurePublic, "tenantId")
            .WithRedirectUri(redirectUri)
            .Build();

        try
        {
            // Interactive authentication flow
            var result = await app.AcquireTokenInteractive(scopes).WithPrompt(Prompt.SelectAccount)
                .ExecuteAsync();
            string accessToken = result.AccessToken;

            // Make HTTP request to Microsoft Graph API
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                var response = await httpClient.GetAsync("https://graph.microsoft.com/beta/appCatalogs/teamsApps");

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Response:\n");

                    // Parse and format JSON response
                    var appsResponse = JsonSerializer.Deserialize<TeamsAppsResponse>(content);
                    foreach (var appInfo in appsResponse.value)
                    {
                        Console.WriteLine($"App ID: {appInfo.id}");
                        Console.WriteLine($"Display Name: {appInfo.displayName}");
                        Console.WriteLine("-----------------------------------------");
                    }
                }
                else
                {
                    Console.WriteLine($"Failed to call Microsoft Graph API. Status code: {response.StatusCode}");
                }
            }
        }
        catch (MsalServiceException)
        {
            throw;
        }
    }
}

// Define classes to represent JSON response
public class TeamsAppsResponse
{
    public List<TeamsApp> value { get; set; }
}

public class TeamsApp
{
    public string id { get; set; }
    public string displayName { get; set; }
}

MFA 登录屏幕:

enter image description here

身份验证成功后,我收到了响应,其中包含应用程序目录中的团队应用程序列表,如下所示:

enter image description here

在您的情况下,将身份验证流程从用户名密码流程更改为交互式流程,其中涉及用户至少登录一次以完成用于生成令牌的 MFA。

© www.soinside.com 2019 - 2024. All rights reserved.