Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonExceptionMiddleware in ASP.NET Core 3 #17

Open
rochapablo opened this issue Jan 29, 2020 · 1 comment
Open

JsonExceptionMiddleware in ASP.NET Core 3 #17

rochapablo opened this issue Jan 29, 2020 · 1 comment

Comments

@rochapablo
Copy link

rochapablo commented Jan 29, 2020

I'm trying to follow the example, but it seems that app.UseExceptionHandler it's called only when I remove app.UseAuthorization, but I do need UseAuthorization. What could I be missing?

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Serialize all exceptions to JSON
    var jsonExceptionMiddleware = new JsonExceptionMiddleware(app.ApplicationServices.GetRequiredService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>());
    app.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = jsonExceptionMiddleware.Invoke });

    app.UseHsts();

    app.UseRouting();

    app.UseHttpsRedirection();            

    app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());            

    app.UseAuthentication();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });            

    app.UseResponseCaching();
}

When I have status 401 for example, It should be calling JsonExceptionMiddleware.Invoke before the request response, but the body it's empty.

@nbarbettini
Copy link
Owner

401 (authorization failure) isn't the same thing as an unhandled exception. UseExceptionHandler is only invoked when an exception is thrown by some code in your application. Authorization failures don't throw exceptions, though.

Are you trying to customize the payload that's returned along with 401?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants