gpt4 book ai didi

c# - Endpoint 包含授权元数据,但未找到支持授权的中间件

转载 作者:行者123 更新时间:2023-12-03 10:04:45 30 4
gpt4 key购买 nike

我目前正在将我本地开发的应用程序移动到 digital ocean 中的 Ubuntu 16.04 液滴。我正在使用 .NET Core 3.1 并且已经为它配置了我的服务器。但是,当我导航到 Controller 上使用 [Authorize] 的端点时属性,我只在我的生产服务器(不是本地)上得到以下异常:

An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Endpoint App.Controllers.RsvpController.Index contains authorization metadata, but a middleware was not found that supports authorization.
Configure your application startup by adding app.UseAuthorization() inside the call to Configure(..) in the application startup code. The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).
at Microsoft.AspNetCore.Routing.EndpointMiddleware.ThrowMissingAuthMiddlewareException(Endpoint endpoint)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.

这就是我的 Configure()方法看起来像 Startup.cs :
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseStaticFiles();

app.UseRouting();

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

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}

我也在 ConfigureServices() 中使用它:
            services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.LoginPath = new PathString("/Account/Login/");
options.AccessDeniedPath = new PathString("/Account/Forbidden/");
});

我的 Controller 有 [Authorize]整个 Controller 类的属性:
    [Authorize]
public class RsvpController : Controller
{
...
}

我无法弄清楚问题是什么,因为它在本地工作。我尝试在本地将 ASPNETCORE_ENVIRONMENT 更改为“生产”,以查看是否有基于此的标志,但我仍然遇到问题。提前感谢您的帮助!

最佳答案

在您的 Configure方法,试试这段代码:

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

关于c# - Endpoint 包含授权元数据,但未找到支持授权的中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61829324/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com