使用 AzureAD 进行 Blazor 身份验证 - 适用于任何组织目录中的帐户

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

我正在尝试设置 Blazor Wasm 应用程序,以便能够从任何组织目录对用户进行身份验证。但是,当用户尝试登录时,如果电子邮件不在 Azure Entra ID 目录的用户列表中,他们会收到错误消息。

这是在program.cs中的调用方式:

 builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

这是appsettings.json。我已经尝试过组织和通用的 TenantId 值。

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com",
    "Domain": "mydomain.onmicrosoft.com",
    "TenantId": "organizations",
    "ClientId": "myclientid012345678",
    "Scopes": "access_as_user",
    "CallbackPath": "/signin-oidc"
  }
}

支持的账户类型有: 任何组织目录中的帐户(任何 Microsoft Entra ID 租户 - 多租户)

authentication azure-active-directory blazor
1个回答
0
投票

AADSTS50020:来自身份提供商“https://sts.windows.net/xxx/”的用户帐户“[电子邮件受保护]”在租户“Contoso”中不存在,并且无法访问应用程序“xxx”(ClientAppRuk)在那个租客里。需要先将该账户添加为租户中的外部用户。

最初我遇到了同样的错误

enter image description here

如果 Microsoft Entra ID 应用程序未配置为 任何组织目录中的帐户(任何 Microsoft Entra ID 租户 - 多租户) 以允许其他租户用户访问,通常会发生此错误。

  • 在代码中,您需要将 TenantId 传递为
    organizations
    .
  • 并确保将 Authority 传递为
    https://login.microsoftonline.com/organizations

我创建了

ServerAppRuk
并公开了如下 API:

enter image description here

创建

ClientAppRuk
并授予API权限:

enter image description here

对于这两个应用程序,请确保选择 “任何组织目录中的帐户(任何 Microsoft Entra ID 租户 - 多租户)” 选项:

enter image description here

要解决该错误,请确保将 Authority 作为

https://login.microsoftonline.com/organizations
传递,如下所示:

客户端应用程序

appsettings.json
文件:

  "AzureAd": {
    "Authority": "https://login.microsoftonline.com/organizations",
    "ClientId": "ClientAppID",
    "ValidateAuthority": true
  }
}

enter image description here

并确保服务器应用程序

appsettings.json
文件如下所示:

{
     "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "XXX.onmicrosoft.com",
    "TenantId": "organizations",
    "ClientId": "ServerAppID",
    "CallbackPath": "RedirectURL",
    "Scopes": "access_as_user"
  },
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*"
}

enter image description here

确保在服务器应用程序中添加客户端应用程序配置,如下所示:

传递客户端应用程序的ClientID

enter image description here

完成上述设置后,我可以使用另一个租户用户成功登录应用程序了:

enter image description here

enter image description here

参考资料:

客户端应用程序配置 (MSAL) - Microsoft 身份平台 |微软

使用 Microsoft Entra ID 保护托管 ASP.NET Core Blazor WebAssembly 应用程序 |微软

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