private Task HandleUnAuthorizeAsync(HttpContext context) { _logService.LogDebug($"UnAuthorize Request"); if (!context.Response.HasStarted) { context.Response.ContentType = "application/json"; context.Response.StatusCode = StatusCodes.Status401Unauthorized; } var model = new { StatusCode = context.Response.StatusCode, Message = "Unauthorized attempt" }; return(context.Response.WriteAsync(model.Serialize())); }
public async Task InvokeAsync(HttpContext httpContext, Services.ILogService logService) { this._logService = logService; try { _logService.LogDebug("Invoked"); await _next(httpContext).ConfigureAwait(false); if (httpContext.Response.StatusCode == StatusCodes.Status401Unauthorized) { await HandleUnAuthorizeAsync(httpContext).ConfigureAwait(false); } } catch (Exception ex) { await HandleExceptionAsync(httpContext, ex).ConfigureAwait(false); } }