.net - IIS 日志失败 : Microsoft. AspNetCore.Server.Kestrel[13] JwtBearerOptions

标签 .net asp.net-core iis asp.net-core-webapi kestrel

在 IIS 中运行已发布的 API 时,出现以下错误:

Hosting environment: Production
Content root path: C:\inetpub\My_API
Now listening on: http://127.0.0.1:44674
Application started. Press Ctrl+C to shut down.
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HM9MTIMQIFTF", Request id "0HM9MTIMQIFTF:00000001": An unhandled exception was thrown by the application.
System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: s
   at System.Text.Encoding.GetBytes(String s)
   at My_API.Startup.<ConfigureServices>b__4_2(JwtBearerOptions options) in D:\repos\API\My_API\Startup.cs:line 173
   at Microsoft.Extensions.Options.ConfigureNamedOptions`1.Configure(String name, TOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass10_0.<Get>b__0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
   at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
   at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
Application is shutting down...

在Startup.cs文件中,生成错误的行代码如下:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => 
options.TokenValidationParameters = new TokenValidationParameters
      {
          ValidateIssuer = false,
          ValidateAudience = false,
          //ValidateLifetime = false,
          ValidateIssuerSigningKey = true,
          //ValidIssuer = "https://locahost",
          //ValidAudience = "https://localhost",
          IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Key_Token"])),
                ClockSkew = TimeSpan.Zero
      });

在开发模式下执行 API 不会出现任何问题,身份验证也没有问题,但在生产模式下执行 API 时,服务器响应状态为 500。

如果有人能帮我解决这个问题。

谢谢你所做的一切。

祝一切顺利。

最佳答案

我已经找到了解决问题的方法。 发生错误的原因是在 Startup.cs 中找不到生成 JWT token 的 key 。在开发环境中,该 key 存储在 appLaunchSettings.json 文件中的项目环境变量中,但是当传递到生产环境时,在 appSettings.json< 中找不到该 key /em> 文件,因此在 StartUp.cs 中找不到该文件时会出现错误。 为了解决这个问题,我已将创建的变量保存在 appLaunchSettings.json 文件中 appSettings.json 文件中,现在一切正常。

谢谢。

关于.net - IIS 日志失败 : Microsoft. AspNetCore.Server.Kestrel[13] JwtBearerOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68115433/

相关文章:

c# - EF Core - 在上一个操作完成之前,在此上下文中启动了第二个操作。不保证任何实例成员都是线程安全的

javascript - 使用 Window.open() 时,ASP Core Javascript 将 http 重定向到 https

amazon-web-services - Elastic Beanstalk 将 iis 站点的 http 重定向到 https

.net - 为什么 BCL GZipStream(带有 StreamReader)不能可靠地检测 CRC32 的数据错误?

c# - 从命令行传递参数时 Devenv 崩溃

asp.net-core - 如何在目录名称中使用句点从 Asp .Net Core 提供静态文件

iis - dotnet 发布错误 : The process cannot access the file because it is being used by another process

c# - 尝试使用 ServerManager 获取应用程序池时出现 COMException

.net - [OperationContract(IsOneWay = true)] 和勾选生成的异步操作复选框有什么区别?

c# - 重新发明标签控件