首页 > 解决方案 > 尝试获取 MS Graph API 的令牌时缺少用户的 ImmutableID(Windows 身份验证)

问题描述

我正在开发一个 API,它使用 Microsoft Graph 客户端 SDK 对 Sharepoint 站点进行多次调用。

为了对调用 API 的用户进行身份验证,我使用了 Integrated Windows Provider 方法,代码非常简单:

var clientApp = PublicClientApplicationBuilder
    .Create(_apiSettings.Value.AzureClientId)
    .WithTenantId(_apiSettings.Value.AzureTenantId)
    .Build();

var token = clientApp.AcquireTokenByIntegratedWindowsAuth(new string[] { _apiSettings.Value.MicrosoftGraphApiScopeUrl })
    .ExecuteAsync().Result;

在本地调用包含此代码的方法时,它可以完美运行。但是,当我将应用程序部署到我们的测试服务器时,并通过与本地用户相同的用户使用 NTLM 身份验证,我遇到了这个错误:

Microsoft.Identity.Client.MsalUiRequiredException:AADSTS90020:SAML 1.1 断言缺少用户的 ImmutableID。

在打印 HttpContext.User 的内容时,我看到同一个用户,连接到同一个组,同时用于“在线”和本地版本。

我已经尝试添加此标头,但它没有做任何事情:

var immId = new Dictionary<string, string>();
immId.Add("Prefer", "IdType=\"ImmutableId\"");

var token = clientApp.AcquireTokenByIntegratedWindowsAuth(new string[] { _apiSettings.Value.MicrosoftGraphApiScopeUrl })
    .WithExtraHttpHeaders(immId)
    .ExecuteAsync().Result;

我能做些什么 ?

谢谢!

标签: c#azure-active-directorywindows-authenticationmsalntlm

解决方案


If your application don't have the admin to consent to the application.You will get the MsalUiRequiredException.

Try with the adding the admin consent to application .

Or Try with adding tenant admin selected the Grant/revoke admin consent for {tenant domain} button on the API permissions tab of the registration for the application. For more information, see Add permissions to access your web API.

For more details refer this document


推荐阅读