NET 6 Blazor应用程序和Azure Active Directory身份验证

xuo3flqw  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(180)

我正在尝试向Blazor服务器和客户端添加Azure Active Directory身份验证。我在Azure中创建了一个新的App Registration

并将 Tenant IDClient ID 复制到我的 * appconfig.json * 中,

  1. {
  2. "AzureAd": {
  3. "Instance": "https://login.microsoftonline.com/",
  4. "Domain": "mycompany.com",
  5. "TenantId": "xxx",
  6. "ClientId": "xxx",
  7. "CallbackPath": "/authentication/login-callback"
  8. },
  9. "Logging": {
  10. "LogLevel": {
  11. "Default": "Information",
  12. "Microsoft.AspNetCore": "Warning"
  13. }
  14. },
  15. "AllowedHosts": "*"
  16. }

我还设置了 * 平台配置 *

现在,我运行服务器应用程序,可以对我的用户进行身份验证。身份验证后,我得到这个错误

我的组织b4e49ba2-xxx与错误中提到的组织完全无关。我创建了一个新的App Registration几次,结果都是一样的。
这是应用程序权限屏幕

30byixjq

30byixjq1#

如果您尚未创建API应用程序并尝试访问该API,则通常会发生此错误。

**若要解决此错误,**请创建Sever应用并公开如下所示的API:

在客户端应用程序中,添加如下所示的API权限:

确保更新Program.cs中的作用域:

  1. builder.Services.AddMsalAuthentication(options =>
  2. {
  3. builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
  4. options.ProviderOptions.DefaultAccessTokenScopes.Add("api://ServerClientID/access_as_user");
  5. });

要解决错误“*”scope“或”scp“声明不包含作用域”API://xxx/ScopeName“或未找到 *.",请确保在ADTest.Serverappsettings.json中将scope更新为作用域名称**而不是api://ServerappId/scopename

  1. "AzureAd": {
  2. "Instance": "https://login.microsoftonline.com/",
  3. "Domain": "xxx.onmicrosoft.com",
  4. "TenantId": "TenantID",
  5. "ClientId": "ClientID",
  6. "CallbackPath": "/authentication/login-callback",
  7. "Scopes": "access_as_user" //scopename
  8. },
  9. "Logging": {
  10. "LogLevel": {
  11. "Default": "Information",
  12. "Microsoft.AspNetCore": "Warning"
  13. }
  14. },
  15. "AllowedHosts": "*"
  16. }

我能够登录Blazor应用程序并成功获取数据:

展开查看全部

相关问题