使用 MSAL.NET 中的设备代码流验证个人和工作/学校帐户

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

我正在开发一个 C# .NET 6.0 桌面应用程序,它应该能够使用 MSAL.NET 中的设备代码流 验证个人(例如 outlook.com)和工作或学校帐户。

在 Microsoft Azure 门户中,我已经注册了应用程序并将支持的帐户类型配置为“任何组织目录中的帐户(任何 Azure AD 目录 - 多租户)和个人 Microsoft 帐户(例如 Skype、Xbox)”。在“身份验证”页面的高级设置中,我还启用了“允许公共客户端流”选项。

根据 release notes MSAL.NET 4.5.0 版支持设备代码流中的两种帐户类型。

在我的应用程序中实现设备代码流时,我一直在关注这个 GitHub 示例。在此示例的 README.md 中明确指出“此示例不适用于 Microsoft 帐户(以前称为 Windows Live 帐户)”。我假设这是指 outlook.com 帐户等。

身份验证确实不适用于 outlook.com 帐户。将代码输入 microsoft.com/devicelogin 登录页面并提供 outlook.com 帐户和密码后,该过程将在此错误消息中停止:

使用属于在其中注册应用程序的同一 Azure AD 租户的帐户可以正常工作。

在设备代码流中启用两种帐户类型的身份验证的要求是什么?

根据设备代码流中列出的约束条件文档需要租用权限。我试图修改

IPublicClientApplication
的实例化(我从 GitHub 中的上述示例复制),如下所示:

PublicClientApplication = PublicClientApplicationBuilder
                .Create("<Application (client) ID>")
                .WithAuthority(@"https://login.microsoftonline.com/<Directory (tenant) ID>")
                .WithDefaultRedirectUri()
                .Build();

这导致登录屏幕出现不同的错误消息:

然而,将租户中的用户添加为外部用户并不是解决此问题的可接受方法。我需要能够验证任何帐户类型,而无需将它们添加到租户。

根据 GitHub 上的这条消息,这种情况下的终点应该是

https://login.microsoftonline.com/{authority}/oauth2/v2.0/devicecode
。我尝试重新配置 JSON 文件(也从 GitHub 中的上述示例复制),如下所示:

{
  "Authentication": {
    // Azure Cloud instance among:
    // - AzurePublic (see https://aka.ms/aaddevv2). This is the default value
    // - AzureUsGovernment (see https://docs.microsoft.com/azure/azure-government/documentation-government-developer-guide)
    // - AzureChina (see https://docs.microsoft.com/azure/china/china-get-started-developer-guide)
    // - AzureGermany (See https://docs.microsoft.com/azure/germany/germany-developer-guide)
    "AzureCloudInstance": "AzurePublic",

    // Azure AD Audience among:
    // - AzureAdMyOrg (single tenant: you need to also provide the TenantId
    // - AzureAdMultipleOrgs (multi-tenant): Any work and school accounts
    // - AzureAdAndPersonalMicrosoftAccount (any work and school account or Microsoft personal account)
    // - PersonalMicrosoftAccount (Microsoft personal account only)
    // "AadAuthorityAudience": "AzureAdMultipleOrgs",
    "Tenant": "common",

    // ClientId (ApplicationId) as copied from the application registration (which depends on the cloud instance)
    // See docs referenced in the AzureCloudInstance section above
    "ClientId": "<Application (client) ID>"
  },

  // Web API. Here Microsoft Graph. The endpoint is different depending on the cloud instance
  // (See docs referenced in the "AzureCloudInstance" section above.
  "WebAPI": {
    "MicrosoftGraphBaseEndpoint": "https://login.microsoftonline.com/common/oauth2/v2.0/devicecode"
  }

}

这没有任何影响,并导致代码过期的相同错误消息。

c# .net msal
© www.soinside.com 2019 - 2024. All rights reserved.