簡體   English   中英

Microsoft.AspNetCore.Authentication.JwtBearer Error when deploying Asp.Net Core with React.js template (do.net SDK v7.0.101) to Azure Web App

[英]Microsoft.AspNetCore.Authentication.JwtBearer Error when deploying Asp.Net Core with React.js template (dotnet SDK v7.0.101) to Azure Web App

我嘗試將帶有 React.js 模板 (do.net SDK v7.0.101) 的 Asp.Net Core 部署到具有“個人帳戶”身份驗證類型的新 Azure Web 應用程序。

應用程序在使用 localhost 時正常運行,但在部署后無法正常運行。 我收到以下錯誤:

[Error] 

Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:
 Exception occurred while processing message.System.NullReferenceException: 
Object reference not set to an instance of an object.at 
Microsoft.AspNetCore.ApiAuthorization.IdentityServer.
IdentityServerJwtBearerOptionsConfiguration.ResolveAuthorityAndKeysAsync(MessageReceivedContext messageReceivedContext)
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()

我懷疑它是 appsettings.json 中 IdentityServer 設置的問題:

    "IdentityServer": {
        "Clients": {
             "AspNetCoreWithReactjs": {
                "Profile": "IdentityServerSPA"
              }
            }
         },

當我使用appsettings.Development.json IdentityServer 設置進行部署時,頁面加載但登錄不起作用。

我希望了解 Asp.Net IdentiyServer 的人可以建議我在 appsettings.json、program.cs 或其他地方需要做什么來解決這個錯誤。

除了連接字符串之外,提供的模板中沒有任何更改,所以我不確定為什么它沒有成功部署。

預期結果:

我期待頁面加載和工作的方式與通過本地主機運行時相同。


重現步驟:

新項目

  • 在 Visual Studio 2022 Community Edition 中創建一個新項目
  • Asp.Net Core 與 React.js模板結合使用

配置您的新項目:

  • 項目名稱:AspNetCoreWithReactjs
  • 位置:C:\Users...\source\repos
  • 解決方案:創建新解決方案
  • 解決方案名稱:AspNetCoreWithReactjs
  • 將解決方案和項目放在同一目錄中:false

附加信息:

  • 框架:.NET 7.0(標准術語支持)
  • 認證類型:個人賬戶
  • 不要使用頂級語句:false

Appsettings.json 連接字符串

  • 將 DefaultConnection 連接字符串更改為使用我現有的 Azure Sql 數據庫
  • 在 Package 管理器控制台中成功運行“更新數據庫”

發布個人資料

  • 目標:Azure
  • 具體目標:Azure 應用服務(Windows)
  • 名稱:AspNetCoreWithReactjs2023010123926
  • 托管計划:美國中部,S1

發布:

  • 配置:發布
  • 目標框架:.net7.0
  • 部署模式:依賴於框架
  • 目標運行時:便攜式

發布后,url 加載出現 500 服務器錯誤:

  • 瀏覽器日志消息:加載資源失敗:服務器響應狀態為 500 ()
  • 請求URL:https://AspNetCoreWithReactjs2023010123926.azurewebsites.net/
  • 請求方式:GET
  • 推薦人政策:嚴格起源時交叉起源
  • 本地存儲:null(無本地存儲對象)
  • Cookies:ARRAffinitySameSite,ARRAffinity

來自 Azure 日志的詳細信息:

Connecting...
2023-01-01T12:58:30  Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds).
2023-01-01 12:58:36.131 +00:00 [Error] Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler: Exception occurred while processing message.System.NullReferenceException: Object reference not set to an instance of an object.at Microsoft.AspNetCore.ApiAuthorization.IdentityServer.IdentityServerJwtBearerOptionsConfiguration.ResolveAuthorityAndKeysAsync(MessageReceivedContext messageReceivedContext)at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
2023-01-01 12:58:36.150 +00:00 [Error] Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Connection ID "17582052946865029492", Request ID "80000176-0000-f400-b63f-84710c7967bb": An unhandled exception was thrown by the application.System.NullReferenceException: Object reference not set to an instance of an object.at Microsoft.AspNetCore.ApiAuthorization.IdentityServer.IdentityServerJwtBearerOptionsConfiguration.ResolveAuthorityAndKeysAsync(MessageReceivedContext messageReceivedContext)at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()
#Software: Microsoft Internet Information Services 8.0
#Fields: date time s-sitename cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Cookie) cs(Referer) cs-host sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken
2023-01-01 12:57:59 ASPNETCOREWITHREACTJS20230101233926 GET / X-ARR-LOG-ID=392d3d5f-4c6c-47ff-b6a1-c99b689a7fb2 443 - 120.148.130.43 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/108.0.0.0+Safari/537.36+Edg/108.0.1462.54 ARRAffinity=146b48f2ff5efabcc69048783c5815d34c57b6d7adedb21b164d0e292cee67ef;+ARRAffinitySameSite=146b48f2ff5efabcc69048783c5815d34c57b6d7adedb21b164d0e292cee67ef https://portal.azure.com/ aspnetcorewithreactjs20230101233926.azurewebsites.net 500 0 0 366 1700 5379
2023-01-01 12:57:59 ~1ASPNETCOREWITHREACTJS20230101233926 GET /api/logstream/ X-ARR-LOG-ID=058aae75-bbeb-442b-adf5-7585174fab8b 443 - 120.148.130.43 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/108.0.0.0+Safari/537.36+Edg/108.0.1462.54 - - aspnetcorewithreactjs20230101233926.scm.azurewebsites.net 200 0 64 672 1603 35233
 

截圖:

步驟1

第2步

第三步

第五步

第七步

Step9

Step10

錯誤圖片3

您是否閱讀過 Microsoft在 Azure App Service with a custom domain and certificate 中針對IdentityServer Host 的帖子? 鑒於IdentityServerJwtBearerOptionsConfiguration.ResolveAuthorityAndKeysAsync失敗,我認為這與此有關。

https://learn.microsoft.com/en-us/as.net/core/blazor/security/webassembly/hosted-with-identity-server?view=as.netcore-7.0&tabs=visual-studio#host-in -azure-app-service-with-a-custom-domain-and-certificate

Blazor WebAssembly編寫,但身份服務器區域仍然相同。

我也在這里寫了一個如何解決的答案:

https://stackoverflow.com/a/66448397/3850405

暫無
暫無

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

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