簡體   English   中英

.NET 5 API 和 React UI 身份驗證使用 Azure AD B2C - Bearer error="invalid_token", error_description="簽名無效"

[英].NET 5 API and React UI authentication using Azure AD B2C - Bearer error="invalid_token", error_description="The signature is invalid"

作為新項目的一部分,我正在嘗試使用 Azure AD B2C 將身份驗證與 React 應用程序和 .NET 5 API 集成。 我想我快到了,但是在發出請求時,我收到一條帶有“Bearer error="invalid_token", error_description="簽名無效"的 401 消息。

  • 我已經在 Azure AD B2C 中注冊了 API 和 React 應用程序。
  • 我已經從 React 應用程序中獲得了 API 的許可。
  • 我創建了一個測試 scope 來啟動
  • 我可以登錄我的 React 應用程序並獲得一個不記名令牌。
  • 如果我檢查令牌,我可以看到觀眾是我的 API 和客戶端 ID。
  • 如果我提出請求,它會將令牌傳遞給 API,正如我所相信的那樣。

在我的 Startup.cs 中,我將其定義如下:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAdB2C"))

我的測試 controller 端點是這樣寫的:

[Authorize]
[HttpGet("private")]
public IActionResult Private()
{
    return Ok(new
    {
        Message = "Hello from a private endpoint! You need to be authenticated to see this."
    });
}

我的 appsettings.json 中有我的 AzureADB2C 設置,如下所示:

"AzureAdB2C": {
    "Instance": "https://login.microsoftonline.com/",
    "ClientId": "<API Client Id (GUID)>",
    /*
      You need specify the TenantId only if you want to accept access tokens from a single tenant
     (line-of-business app).
      Otherwise, you can leave them set to common.
      This can be:
      - A GUID (Tenant ID = Directory ID)
      - 'common' (any organization and personal accounts)
      - 'organizations' (any organization)
      - 'consumers' (Microsoft personal accounts)
    */
    "TenantId": "common"
  },

I'm wrestling with a mixture of out of date documentation in some cases and the examples given use a node.js API with no .NET API examples so I've tried to piece together everything as best I can and I'm not quite sure what I我失蹤了。

如果要使用 Net Core Web API 和 Azure AD B2C,我們需要將 appsetting.ZD6466DEEC7F6ECDF25 更新如下

 "AzureAdB2C": {
    "Instance": "https://xxx.b2clogin.com",
    "ClientId": "",
    "Domain": "xxx.onmicrosoft.com",
    "SignUpSignInPolicyId": "B2C_1A_signup_signin"
  },

更多詳細信息,請參閱博客博客示例

此外,關於如何將 Azure AD B2C 與 React 應用程序集成,請參閱此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM