【问题标题】:IIS log fail: Microsoft.AspNetCore.Server.Kestrel[13] JwtBearerOptionsIIS 日志失败:Microsoft.AspNetCore.Server.Kestrel[13] JwtBearerOptions
【发布时间】:2021-09-07 22:18:36
【问题描述】:

在 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。

如果有人可以帮我解决问题。

感谢一切。

一切顺利。

【问题讨论】:

  • 您可以使用失败请求跟踪来查看详细的错误信息。

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


【解决方案1】:

我找到了解决问题的方法。 发生错误是因为在 Startup.cs 中找不到生成 JWT 令牌的密钥。在开发环境中,密钥存储在 appLaunchSettings.json 文件中的项目环境变量中,但是当传递到生产环境时,在 appSettings.json 文件,因此在 StartUp.cs 中找不到它时会给出错误。 为了解决这个问题,我将创建的变量保存在 appSettings.json 文件中的 appLaunchSettings.json 文件中,现在一切正常。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 2022-01-12
    • 2021-04-02
    • 2015-02-15
    • 2023-04-04
    • 2010-12-17
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多