c# - Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT 中间件 C# .NET Core 2.2

标签 c# .net asp.net-core middleware jwt-auth

我有一个 .NET Core 2.2 C# API,它随机崩溃并出现以下情况:

2020-08-07 10:44:42.039 +00:00 [Error] Exception occurred while processing message.
System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.Threading.SemaphoreSlim.WaitUntilCountOrTimeoutAsync(TaskNode asyncWaiter, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
2020-08-07 10:44:42.062 +00:00 [Error] Connection ID ""17798225728978863173"", Request ID ""8000c446-0000-f700-b63f-84710c7967bb"": An unhandled exception was thrown by the application.
System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.Threading.SemaphoreSlim.WaitUntilCountOrTimeoutAsync(TaskNode asyncWaiter, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
   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.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.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

我无法在另一个环境中重现此问题。在我看来,中间件(或者可能是身份验证)存在问题

我继承了该项目并注意到中间件的顺序可能不正确: 启动.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // swagger
            app.UseSwagger();
            app.UseSwaggerUI(options => options.ConfigureSwaggerUi(this.Configuration));

            // versioning
            app.UseCustomApiVersionHeader(this.Configuration);

            // authentication
            app.UseAuthentication();

            // static files
            app.UseStaticFiles();

            app.UseMvc();
        }

我还有一个自定义身份验证服务:

public void ConfigureServices(IServiceCollection services)
 {
...
services.AddCustomAuthentication(this.Configuration);
...
}

AuthenticationIoC:

namespace Authentication.Api.Middleware
{
    public static class CustomAuthenticationIoc
    {
        public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
        {
            var domain = $"https://{configuration["security:domain"]}/";

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.Authority = $"https://{configuration["security:domain"]}/";
                options.Audience = configuration["security:audience"];
            });

            // get all policies
            var policies = configuration["security:policies"]?
                .Split(' ', StringSplitOptions.RemoveEmptyEntries)?
                .ToList();

            services.AddAuthorization(configure: options =>
            {
                foreach (var policyItem in policies)
                {
                    options.AddPolicy(policyItem,
                        policy => policy.Requirements.Add(new HasScopeRequirement(policyItem, domain)));
                }
            });

            // register the scope authorization handler
            services.AddSingleton<IAuthorizationHandler, HasScopeHandler>();

            return services;
        }
    }
}

任何帮助将不胜感激,因为我已经探索了许多不同的根源:环境、身份验证和 JWT Swagger 等。

最佳答案

当客户端(浏览器,应用程序)取消请求时发生,然后您的网络应用程序取消此请求,作业。

关于c# - Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT 中间件 C# .NET Core 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63301078/

相关文章:

Docker compose 构建错误 - 项目文件不存在

c# - EntityFramework EntityCollection 观察 CollectionChanged

c# - "Padding is invalid and cannot be removed"使用 AesManaged

asp.net - MVC 巨大模型性能不佳

.net - 我可以创建一个没有项目文件的 nuget 包吗

c# - Newtonsoft.Json System.InvalidOperationException : Synchronous operations are disallowed

c# - 在 .NET Core 中使用数据表

c# - LINQ,SQLite,分组和计数

c# - 使用 ASP.NET 删除和更新 Google 事件 [Google 日历]

c# - asp.net 中 DateTime 的 Javascript 序列化没有提供 javascript 日期对象?