呼叫 Web API 的桌面應用程式:使用整合式 Windows 驗證 取得令牌
若要在網域或已加入 Microsoft Entra 的電腦上登入網域使用者,請使用整合式 Windows 驗證 (IWA)。
條件約束
整合式 Windows 驗證 僅適用於同盟+ 使用者,也就是在 Active Directory 中建立並受 Microsoft Entra ID 支援的使用者。 直接在 Microsoft Entra 識別符中建立的使用者沒有 Active Directory 備份,稱為 受控 使用者,無法使用此驗證流程。 這項限制不會影響使用者名稱和密碼流程。
IWA 不會略過多重要素驗證(MFA)。 如果已設定 MFA,則需要 MFA 挑戰時,IWA 可能會失敗,因為 MFA 需要用戶互動。
IWA 不是互動式的,但 MFA 需要用戶互動。 租用戶系統管理員不會控制識別提供者要求執行 MFA 的時機。 從我們的觀察中,當您從不同的國家/地區登入、未透過 VPN 連線到公司網路時,有時甚至是透過 VPN 連線時,都需要 MFA。 不要預期一組具決定性的規則。 Microsoft Entra ID 會使用 AI 持續瞭解是否需要 MFA。 如果 IWA 失敗,請回復至使用者提示,例如互動式驗證或裝置程式代碼流程。
傳入的
PublicClientApplicationBuilder
授權單位必須是:- 租使用者格式
https://login.microsoftonline.com/{tenant}/
為 ,其中tenant
是代表租使用者標識碼的 GUID 或與租使用者相關聯的網域。 - 針對任何工作和學校帳戶:
https://login.microsoftonline.com/organizations/
。 - 不支援 Microsoft 個人帳戶。 您無法使用 /common 或 /consumers 租使用者。
- 租使用者格式
因為整合式 Windows 驗證 是無訊息流程:
- 您應用程式的使用者先前必須已同意使用應用程式。
- 或者,租用戶系統管理員先前必須已同意租使用者中的所有使用者使用應用程式。
- 換句話說:
- 身為開發人員的您自己在 Azure 入口網站 中選取 [授與] 按鈕。
- 或者,租用戶系統管理員在應用程式註冊的 [API 許可權] 索引卷標上選取 [授與/撤銷 {tenant domain} 的管理員同意] 按鈕。 如需詳細資訊,請參閱 新增許可權以存取您的Web API。
- 或者,您已為使用者提供同意應用程式的方式。 如需詳細資訊,請參閱 要求個別使用者同意。
- 或者,您已為租用戶系統管理員提供同意應用程式的方式。 如需詳細資訊,請參閱 管理員 同意。
此流程已針對 .NET 桌面、.NET 和 UWP 應用程式啟用。
如需同意的詳細資訊,請參閱 Microsoft 身分識別平台 許可權和同意。
瞭解如何使用它
在 MSAL.NET 中,使用:
AcquireTokenByIntegratedWindowsAuth(IEnumerable<string> scopes)
您通常只需要一個參數 (scopes
)。 視 Windows 系統管理員設定原則的方式而定,您 Windows 電腦上的應用程式可能無法查閱已登入的使用者。 在這裡情況下,請使用第二個方法 .WithUsername()
,並以 UPN 格式傳入已登入使用者的使用者名稱,例如 joe@contoso.com
。
下列範例會呈現最新的案例,並說明您可以取得的例外狀況種類及其風險降低。
static async Task GetATokenForGraph()
{
string authority = "https://login.microsoftonline.com/contoso.com";
string[] scopes = new string[] { "user.read" };
IPublicClientApplication app = PublicClientApplicationBuilder
.Create(clientId)
.WithAuthority(authority)
.Build();
var accounts = await app.GetAccountsAsync();
AuthenticationResult result = null;
if (accounts.Any())
{
result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
else
{
try
{
result = await app.AcquireTokenByIntegratedWindowsAuth(scopes)
.ExecuteAsync(CancellationToken.None);
}
catch (MsalUiRequiredException ex)
{
// MsalUiRequiredException: AADSTS65001: The user or administrator has not consented to use the application
// with ID '{appId}' named '{appName}'.Send an interactive authorization request for this user and resource.
// you need to get user consent first. This can be done, if you are not using .NET (which does not have any Web UI)
// by doing (once only) an AcquireToken interactive.
// If you are using .NET or don't want to do an AcquireTokenInteractive, you might want to suggest the user to navigate
// to a URL to consent: https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={clientId}&response_type=code&scope=user.read
// AADSTS50079: The user is required to use multi-factor authentication.
// There is no mitigation - if MFA is configured for your tenant and AAD decides to enforce it,
// you need to fallback to an interactive flows such as AcquireTokenInteractive or AcquireTokenByDeviceCode
}
catch (MsalServiceException ex)
{
// Kind of errors you could have (in ex.Message)
// MsalServiceException: AADSTS90010: The grant type is not supported over the /common or /consumers endpoints. Please use the /organizations or tenant-specific endpoint.
// you used common.
// Mitigation: as explained in the message from Azure AD, the authority needs to be tenanted or otherwise organizations
// MsalServiceException: AADSTS70002: The request body must contain the following parameter: 'client_secret or client_assertion'.
// Explanation: this can happen if your application was not registered as a public client application in Azure AD
// Mitigation: in the Azure portal, edit the manifest for your application and set the `allowPublicClient` to `true`
}
catch (MsalClientException ex)
{
// Error Code: unknown_user Message: Could not identify logged in user
// Explanation: the library was unable to query the current Windows logged-in user or this user is not AD or AAD
// joined (work-place joined users are not supported).
// Mitigation 1: on UWP, check that the application has the following capabilities: Enterprise Authentication,
// Private Networks (Client and Server), User Account Information
// Mitigation 2: Implement your own logic to fetch the username (e.g. john@contoso.com) and use the
// AcquireTokenByIntegratedWindowsAuth form that takes in the username
// Error Code: integrated_windows_auth_not_supported_managed_user
// Explanation: This method relies on a protocol exposed by Active Directory (AD). If a user was created in Azure
// Active Directory without AD backing ("managed" user), this method will fail. Users created in AD and backed by
// AAD ("federated" users) can benefit from this non-interactive method of authentication.
// Mitigation: Use interactive authentication
}
}
Console.WriteLine(result.Account.Username);
}
如需 AcquireTokenByIntegratedWindowsAuthentication 上可能的修飾詞清單,請參閱 AcquireTokenByIntegratedWindowsAuthParameterBuilder。
下一步
請移至此案例中的下一篇文章, 從傳統型應用程式呼叫Web API。