diff --git a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs index 9f0a665c3d..8a5aad25bf 100644 --- a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs @@ -15,11 +15,27 @@ namespace JsonApiDotNetCore.Extensions public static class IApplicationBuilderExtensions { /// - /// Adds necessary components such as routing to your application + /// Runs several internal JsonApiDotNetCore services to ensure proper configuration and registers required middlewares. + /// The can be used to skip any middleware registration, in which case the developer is + /// is responsible for registering middleware that are required for JsonApiDotNetCore. /// /// - /// - public static void UseJsonApi(this IApplicationBuilder app) + /// Indicates if JsonApiDotNetCore should skip middleware registration. This enabl. + /// Indicates if .NET Core authentication middleware should be registered. Ignored when is set to true. + /// Indicates if .NET Core authentication middleware should be registered. Ignored when is set to true. + /// + /// This example illustrate which required middlewares should be registered when using the option. + /// + /// app.UseJsonApi(skipRegisterMiddleware: true); + /// // JADNC requires routing + /// app.UseRouting(); + /// // JADNC requires CurrentRequestMiddleware + /// app.UseMiddleware(); + /// // JANDC requires the endpoint feature enabled as follows + /// app.UseEndpoints(endpoints => endpoints.MapControllers()); + /// + /// + public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMiddleware = false, bool useAuthentication = false, bool useAuthorization = false) { LogResourceGraphValidations(app); using (var scope = app.ApplicationServices.CreateScope()) @@ -28,14 +44,26 @@ public static void UseJsonApi(this IApplicationBuilder app) inverseRelationshipResolver?.Resolve(); } - // An endpoint is selected and set on the HttpContext if a match is found - app.UseRouting(); + if (!skipRegisterMiddleware) { + // An endpoint is selected and set on the HttpContext if a match is found + app.UseRouting(); - // middleware to run after routing occurs. - app.UseMiddleware(); + if (useAuthentication) + { + app.UseAuthentication(); + }; - // Executes the endpoints that was selected by routing. - app.UseEndpoints(endpoints => endpoints.MapControllers()); + if (useAuthorization) + { + app.UseAuthorization(); + }; + + // middleware to run after routing occurs. + app.UseMiddleware(); + + // Executes the endpoints that was selected by routing. + app.UseEndpoints(endpoints => endpoints.MapControllers()); + } } ///