Beispiel #1
0
 private static IActionResult GetError(ErrorResponseWithState error, HttpStatusCode httpStatusCode)
 {
     return(new JsonResult(error)
     {
         StatusCode = (int)httpStatusCode
     });
 }
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception exception)
            {
                var simpleIdentityServerEventSource  = _options.SimpleIdentityServerEventSource;
                var identityServerException          = exception as IdentityServerException;
                var identityServerExceptionWithState = exception as IdentityServerExceptionWithState;
                if (identityServerException == null)
                {
                    identityServerException = new IdentityServerException(ErrorCodes.UnhandledExceptionCode, exception.Message);
                    simpleIdentityServerEventSource.Failure(exception);
                }
                else
                {
                    var code    = identityServerException.Code;
                    var message = identityServerException.Message;
                    var state   = identityServerExceptionWithState == null
                        ? string.Empty
                        : identityServerExceptionWithState.State;
                    simpleIdentityServerEventSource.OpenIdFailure(code, message, state);
                }

                context.Response.Clear();
                if (identityServerExceptionWithState != null)
                {
                    var errorResponseWithState = new ErrorResponseWithState
                    {
                        State = identityServerExceptionWithState.State
                    };

                    PopulateError(errorResponseWithState, identityServerExceptionWithState);
                    context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                    context.Response.ContentType = "application/json";
                    var serializedError = errorResponseWithState.SerializeWithDataContract();
                    await context.Response.WriteAsync(serializedError);
                }
                else
                {
                    var error = new ErrorResponse();
                    PopulateError(error, identityServerException);
                    context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                    context.Response.ContentType = "application/json";
                    var serializedError = error.SerializeWithDataContract();
                    await context.Response.WriteAsync(serializedError);
                }
            }
        }