处理错误并处理不完整的身份验证上下文



我正试图处理有关抛出的异常和返回的HTTP状态码的错误。我想两个都要。

这是我目前在Program.cs文件中的内容:

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();

我得到一个错误,不知道为什么,我认为这可能是由于我的"应用程序"的顺序。我不知道如何重新订购它,使它工作。主页没有加载,它只是返回一个500的错误,错误如下:

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

我不知道如何重新安排项目的顺序,请问有人可以帮助吗?

我目前使用的是Visual Studio 2022.NET的最新版本。

我能够通过以下设置解决问题:

app.UseAuthentication();
app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");
app.UseExceptionHandler("/Home/Error");
app.UseAuthorization();

最新更新