Pular para o conteúdo principal
Versão: 10.4.1

ITLab.Template.DevPack.Middleware

Provides ASP.NET Core middleware for culture negotiation, 404 handling and unauthenticated request fallback.

Componentes

TipoNomeResponsabilidade
ClassCultureMiddlewareSets request culture from the Accept-Language header (fallback: en-US)
Sealed ClassDevPackIdentityContextMiddlewareBridges HttpContext.User claims into AsyncLocalCurrentUserAccessor so ICurrentUser consumers see the authenticated user
ClassNotFoundMiddlewareIntercepts 404 responses and returns a structured error payload
ClassUnauthenticatedFallbackMiddlewareReturns 401 Unauthorized for requests that reach the pipeline without an identity

Como usar

// Register middleware in the correct pipeline order.
app.UseMiddleware<CultureMiddleware>();
app.UseAuthentication();
app.UseDevPackIdentityContext(); // bridge HttpContext.User → AsyncLocal
app.UseMiddleware<UnauthenticatedFallbackMiddleware>();
app.UseAuthorization();
app.UseMiddleware<NotFoundMiddleware>();

Order matters: UseAuthentication populates HttpContext.User; UseDevPackIdentityContext projects the claims into AsyncLocalCurrentUserAccessor (consumed by RoleBasedPermissionEvaluator and other ICurrentUser consumers); UseAuthorization then evaluates policies. Without the bridge, ICurrentUser.UserRoles is empty in production → 403 on every protected endpoint.

O que não fazer

  • Do not place CultureMiddleware after UseRouting — culture must be set before route handlers execute.
  • Do not use UnauthenticatedFallbackMiddleware as a substitute for proper [Authorize] annotations.

Extensão pelo produto

  • Products can extend NotFoundMiddleware to include product-specific 404 response bodies.
  • Products can extend CultureMiddleware to resolve culture from user profile settings instead of the request header.