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