'Handling errors and dealing with an incomplete authentication context

I am trying to handle errors with regards to exceptions thrown and HTTP status codes returned. I would like to handle both.

This is what I currently have in my Program.cs file:

var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;

services.AddControllersWithViews();

services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
    .AddNegotiate();
services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});

var app = builder.Build();

if (!app.Environment.IsDevelopment())
    app.UseExceptionHandler("/Home/Error");
app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");

app.UseStaticFiles();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

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

app.Run();

I am getting an error and not sure why, I am thinking it might be due to the order of my "app" items, I am not sure how to reorder it so that it works. The home page doesn't load, it just returns a 500 error with the following:

InvalidOperationException: Attempting to use an incomplete authentication context.
Microsoft.AspNetCore.Authentication.Negotiate.NegotiateHandler.HandleAuthenticateAsync()

I am not sure how to rearrange the order of the items, please can someone assist?

I am currently using the lastest version of Visual Studio 2022 and .NET.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source