【问题标题】:Handling errors and dealing with an incomplete authentication context处理错误和处理不完整的身份验证上下文
【发布时间】:2022-11-11 09:50:28
【问题描述】:

我正在尝试处理有关引发的异常和返回的 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

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-11
    • 2016-06-12
    • 1970-01-01
    • 2018-09-13
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多