簡體   English   中英

ASP.NET 核心與 Azure 上的 React 和 IdentityServer 4:承載錯誤 =“invalid_token”,error_description =“發行者無效”

[英]ASP.NET Core with React and IdentityServer 4 on Azure: Bearer error="invalid_token", error_description="The issuer is invalid"

我使用ASP.NET Core with React.js Visual Studio 2022 模板創建了 .NET 6 應用程序。 然后我將 TypeScript 添加到客戶端應用程序部分。 我也在使用 IdentityServer 4。

這是我在Startup.cs中注冊 IdentityServer 的方法:

services.AddIdentityServer()
                .AddAspNetIdentity<ApplicationUser>()
                .AddIdentityResources()
                .AddApiResources()
                .AddClients()
                .AddDeveloperSigningCredential();

這是appsettings.json的我的IdentityServer部分:

"IdentityServer": {
    "Clients": {
      "MyApp.App": {
        "Profile": "IdentityServerSPA"
      }
    }
  }

我有這個樣本WeatherForecastController ,它包含在模板中,上面有[Authorize]屬性。

在本地一切正常,但是一旦我部署到 Azure(通過 BitBucket 提交部署),我可以毫無問題地注冊/登錄到應用程序,但是在嘗試訪問授權路線時,我收到401 Unauthorized錯誤消息:

Bearer error="invalid_token", error_description="The issuer 'https://myapp.azurewebsites.net' is invalid"

根據這個問題,我將我的應用程序的 Azure URL 添加到appsettings.json

"IdentityServer": {
    "IssuerUri": "https://myapp.azurewebsites.net",
    "Clients": {
      "MyApp.App": {
        "Profile": "IdentityServerSPA"
      }
    }
  }

但這沒有幫助。

然而,有幫助的是在代碼中定義這個 URI:

services.AddIdentityServer(options =>
                {
                    options.IssuerUri = "https://myapp.azurewebsites.net";
                })
                .AddAspNetIdentity<ApplicationUser>()
                .AddIdentityResources()
                .AddApiResources()
                .AddClients()
                .AddDeveloperSigningCredential();

但是,這似乎是一種代碼氣味。 正如IdentityServer 文檔所說:

建議不要設置此屬性,它會根據客戶端使用的主機名推斷頒發者名稱。

另外,這有點奇怪,因為我需要在本地主機上開發時在本地更改 C# 中的這個 URI 以使一切正常工作。 當然,我可以將它提取到appconfig.json ,但我覺得它仍然很奇怪,它是必需的。

我不是很了解這個問題,所以我一直在尋找更多。

我發現其他問題建議將我的令牌(在授權失敗時獲得)粘貼到https://jwt.ms/ 中,這是 output:

{
  "alg": "RS256",
  "kid": "7BEDB584D24C2C0D6619ED5C802A4EEF",
  "typ": "at+jwt"
}.{
  "nbf": 1638590243,
  "exp": 1638593843,
  "iss": "https://myapp.azurewebsites.net",
  "aud": "MyApp.AppAPI",
  "client_id": "MyApp.App",
  "sub": "d611ae4d12614a94aeac0399fac81b3b",
  "auth_time": 1638590237,
  "idp": "local",
  "jti": "648F96B7DA864DB28FB06540325310A6",
  "sid": "6187CB0CF53F3049062BD8B7728F6C68",
  "iat": 1638590243,
  "scope": [
    "MyApp.AppAPI",
    "openid",
    "profile"
  ],
  "amr": [
    "pwd"
  ]
}.[Signature]

這里唯一讓我感到驚訝的是這個MyApp.AppAPI - 我不知道這個AppAPI部分是什么。 我的 ASP.NET 核心解決方案或客戶端應用程序中沒有類似的東西。 這是 Azure 特有的東西嗎? 也許這就是這里的問題?

任何簡單英語的幫助將不勝感激 - 我對 Azure 很陌生

對於 Identity Server,我不是 100% 確定,但通常,您還需要在Startup.cs中添加身份驗證

services.AddAuthentication(opt =>
{
    opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(opt =>
{
    opt.TokenValidationParameters = new()
    {
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidIssuer = "https://myapp.azurewebsites.net",
        ValidAudience = "https://myapp.azurewebsites.net"
     };
 });

而ValidIssuer中的值顯然需要與JWT中使用的issuer相匹配

暫無
暫無

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

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