大网数据广告活动
域与目录服务

MsalClientException - Failed to Get User Name

· 52CE 我爱测

Resolves the Failed to get user name error when an application uses Integrated Windows Authentication (IWA) with Microsoft Authentication Library (MSAL).

Summary

This article provides a solution to the "Failed to get user name" error that occurs when an application uses Integrated Windows Authentication (IWA) together with Microsoft Authentication Library (MSAL).

Symptoms

When your application uses IWA together with MSAL, if calling the AcquireTokenByIntegratedWindowsAuth method as follows:

result = await app.AcquireTokenByIntegratedWindowsAuth(scopes)

You encounter one of the following errors:

  • Microsoft.Identity.Client.MsalClientException: Failed to get user name —>
    System.ComponentModel.Win32Exception: No mapping between account names and security IDs was done

  • Microsoft.Identity.Client.MsalClientException: Failed to get user name —>
    System.ComponentModel.Win32Exception: Access Denied

Cause

The error originates from Windows. It occurs because MSAL calls the GetUserNameEx function from secur32.dll. For more information, see MSAL WindowsNativeMethods.cs - GetUserNameEx.

Solution

Note: Before you begin, ensure the following minimum requirements are met:

  • Run the application as a local Active Directory user, not a local computer user account.
  • The device running the application is joined to the domain.

To resolve this issue, pass the username to AcquireTokenByIntegratedWindowsAuth.

If the username is known beforehand, you can manually pass it to MSAL as follows:

result = await app.AcquireTokenByIntegratedWindowsAuth(scopes).WithUsername("<service-account>@contoso.com")

If the username isn't known beforehand, dynamically retrieve the username and then pass it to AcquireTokenByIntegratedWindowsAuth by using one of the following methods:

  • Use System.Security.Principal.WindowsIdentity.GetCurrent()

    Here's the code example:

    string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    result = await app.AcquireTokenByIntegratedWindowsAuth(scopes).WithUsername(username)
    

    [!NOTE] If the returned username doesn't include a domain, this method fails and returns different errors. For proper integration with Microsoft Entra ID, you must pass the username in the format of a user principal name.

  • Use PublicClientApplication.OperatingSystemAccount.Username

    Here's the code example:

    string username = PublicClientApplication.OperatingSystemAccount.Username;
    result = await app.AcquireTokenByIntegratedWindowsAuth(scopes).WithUsername(username)
    

    [!NOTE] This method tries to access the Windows Account Broker to sign the user into the device. It doesn't work if the application runs on Internet Information Services (IIS) or Windows Server.

Reference

Using MSAL.NET with Integrated Windows Authentication (IWA)

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 07/01/2025
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。