From 239cd8bcbb9b2f26f997d8592297926b03434a0d Mon Sep 17 00:00:00 2001 From: xRain Date: Wed, 14 Sep 2022 04:56:21 +0800 Subject: [PATCH] fix error when exception response has started --- Attributes/SimApiAuthAttribute.cs | 2 +- Middlewares/SimApiExceptionMiddleware.cs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Attributes/SimApiAuthAttribute.cs b/Attributes/SimApiAuthAttribute.cs index 484a621..7a652ee 100644 --- a/Attributes/SimApiAuthAttribute.cs +++ b/Attributes/SimApiAuthAttribute.cs @@ -56,7 +56,7 @@ namespace SimApi.Attributes throw new SimApiException(401); } //检测用户类型 - if (Types.Intersect(loginInfo.Type).Count() == 0) + if (!Types.Intersect(loginInfo.Type).Any()) { throw new SimApiException(403); } diff --git a/Middlewares/SimApiExceptionMiddleware.cs b/Middlewares/SimApiExceptionMiddleware.cs index 39d1d33..74b0ce7 100644 --- a/Middlewares/SimApiExceptionMiddleware.cs +++ b/Middlewares/SimApiExceptionMiddleware.cs @@ -14,6 +14,7 @@ namespace SimApi.Middlewares public class SimApiExceptionMiddleware { private RequestDelegate Next { get; } + private ILogger Log { get; } public SimApiExceptionMiddleware(RequestDelegate next, ILogger log) @@ -35,7 +36,10 @@ namespace SimApi.Middlewares await Next(context); if (context.Response.StatusCode != 200) { - if (!new[] {301, 302}.Contains(context.Response.StatusCode)) + if (!new[] + { + 301, 302 + }.Contains(context.Response.StatusCode)) { throw new SimApiException(context.Response.StatusCode); } @@ -70,9 +74,12 @@ namespace SimApi.Middlewares /// private void ErrorResponse(HttpContext context, SimApiBaseResponse response) { - context.Response.StatusCode = 200; - context.Response.Headers.Add("Content-Type", "application/json"); - context.Response.WriteAsync(response.ToString()); + if (!context.Response.HasStarted) + { + context.Response.StatusCode = 200; + context.Response.Headers.Add("Content-Type", "application/json"); + context.Response.WriteAsync(response.ToString()); + } } } } \ No newline at end of file