我目前正在将本地开发的应用程序迁移到数字海洋中的Ubuntu 16.04 droplet。我使用的是.NET Core 3.1,并已将服务器配置得很好。但是,当我导航到使用[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/");
});
我的控制器在整个控制器类周围有[Authorize]
属性:
[Authorize]
public class RsvpController : Controller
{
...
}
我无法找出问题所在,因为它在本地工作。我尝试在本地将ASPNETCORE_ENVIRONMENT更改为“Production”,以查看是否有基于此的标记,但我仍然遇到问题。提前感谢您的帮助!
4条答案
按热度按时间jyztefdp1#
在
Configure
方法中,尝试以下代码:lc8prwob2#
解决这个问题的最简单方法是将app.UseAuthorization()移动到app.UseRouting()和app.UseEndpoints()之间的任意位置,如下例所示:
7qhs6swi3#
在我的情况下,我使用这个命令,它工作了
我像
app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
一样使用CORS,因为我只是在开发应用程序,您可以在CORS配置中使用您想要的内容,但中间件的顺序确实很重要。请参见链接@Massimo Provide。我使用该链接来找出解决方案csbfibhn4#
thx. mesut它的工作