Actualmente estoy en el proceso de mover mi aplicación desarrollada localmente a una gota de Ubuntu 16.04 en el océano digital. Estoy usando .NET Core 3.1 y configuré mi servidor perfectamente. Sin embargo, cuando navego a un punto final en mi controlador que usa el atributo [Authorize] , obtengo la siguiente excepción solo en mi servidor de producción (no localmente):
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. Así es como se ve mi método Configure() en 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?}"); }); } También estoy usando esto en 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/"); }); Mi controlador tiene el atributo [Authorize] alrededor de toda la clase de controlador:
[Authorize] public class RsvpController : Controller { ... }No puedo entender cuál es el problema, ya que funciona localmente. Intenté cambiar ASPNETCORE_ENVIRONMENT a "Producción" localmente para ver si había un indicador en algún lugar basado en eso, pero sigo teniendo el problema. ¡Gracias de antemano por cualquier ayuda!
En su método Configure , pruebe este fragmento de código:
... app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });La solución muy fácil para esto es mover app.UseAuthorization() a cualquier lugar entre app.UseRouting() y app.UseEndpoints() como en este ejemplo:
app.UseRouting(); //Enable Authentication app.UseAuthentication(); app.UseAuthorization(); //<< This needs to be between app.UseRouting(); and app.UseEndpoints(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });en mi caso uso esta orden y funciono
app.UseRouting(); app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); Uso CORS como app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); Debido a que solo estoy desarrollando la aplicación, puede usar lo que quiera en la configuración de CORS, pero el orden del Middleware realmente importa. consulte el enlace @Massimo Provide. Utilizo ese enlace para encontrar la solución.