【问题标题】:Setting up Blazor WebAssembly App with API with AzureAd in Visual Studio 2022在 Visual Studio 2022 中使用 API 和 AzureAd 设置 Blazor WebAssembly 应用程序
【发布时间】:2022-01-26 17:59:01
【问题描述】:

我是 Blazor 和 Azure Ad 的新手(或者实际上是生成身份验证),我想从 Visual Studio 2022 中的向导生成的应用程序中设置一个简单的基本应用程序。我能够完成身份验证部分并运行,但我无法访问 API。我收到 401 - 响应标头中的未经授权和 www-authenticate 是:Bearer error="invalid_token", error_description="签名无效"

这是我所做的:

  1. 在 Visual Studio 2022 中,使用 Blazor WebAssembly 应用创建一个新项目
    • 将框架保持为 .Net 6.0(长期支持)
    • 将身份验证类型更改为 Microsoft 身份平台
    • 保持“为 HTTPS 配置”开启
    • 选中“ASP.Net Core 托管”
    • 关闭“渐进式 Web 应用程序”
  2. 我确认我要通过单击完成按钮安装“dotnet msidentity 工具”
  3. 在 Azure “应用注册”部分,我选择顶部的“新注册”
    • 我输入“MyBlazorApp1”作为名称
    • 为支持的类型选择“仅限个人 Microsoft 帐户”
    • 将重定向 URI 设置为 SPA 和 https://localhost:7095/authentication/login-callback
    • 点击左下角注册按钮
    • Screen capture of the filled fields
  4. Screen capture of the created AzureAd application
  5. 在 BlazorApp1.Client/wwwroot/appsettings.json 中:
  6. 在 BlazorApp1.Server/appsettings.json 中:
    • 将域设置为“主域”,找到“Azure Active Directory”概述页面。
    • 将 TenantId 设置为租户 ID(以上屏幕截图中的 3)
    • 将客户端 ID 设置为客户端 ID(以上屏幕截图中的 1)
  7. 执行应用程序 (Ctrl-F5)
  8. 使用屏幕右上角的登录,一切正常!
  9. 进入左侧的获取数据,我收到“发生未处理的错误。屏幕底部的“重新加载”。
  10. 在 Azure 上,在之前创建的应用程序的“公开 API”部分中,单击“添加范围”
    • 我接受由客户端 ID 生成的默认应用程序 ID URI,点击“保存并继续”
    • 将范围名称设置为“access_as_user”
    • 将用户常量显示名称设置为“以用户身份访问”
    • 将用户同意描述设置为“作为用户描述访问”
    • 保持状态为已启用
    • 然后点击“添加范围”
    • Screen capture of the fields to create the scope
  11. 在 BlazorApp1.Client/Program.cs 中:
    • 将“api://api.id.uri/access_as_user”更改为 api://Client ID/access_as_user(以上屏幕截图中的 4)
  12. 登录,进入左侧的获取数据部分,仍然出现未处理错误。重新加载”在屏幕底部。查看 Edge DevTools 的 Network 部分,我可以看到:
    • WeatherForecast 返回 401
    • 响应头中的www-authenticate为:Bearer error="invalid_token", error_description="签名无效"
    • DevTools screen capture

我觉得我错过了一些你们可能会发现很明显的东西,但这是我第一次使用 AzureAd 的 Blazor 我必须承认我不确定在哪里继续搜索......

谢谢!

【问题讨论】:

    标签: api azure-active-directory blazor webassembly visual-studio-2022


    【解决方案1】:

    经过大量搜索,我找到了解决方案(在https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp,第 3 步)。事实证明,当您对应用程序类型使用“仅个人 Microsoft 帐户”时,您不应在 BlazorApp1.Server/appsettings.json 中指定租户 ID,而应简单地指定“自定义”。

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 2020-06-28
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 2021-12-25
      • 2020-06-05
      相关资源
      最近更新 更多