From 47cfdb5cc606976300b7c8791d4a90bf9ffbdc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Tue, 14 Jan 2020 15:01:13 +0100 Subject: [PATCH 1/5] Add optional param to add user middleware after routing --- .../Extensions/IApplicationBuilderExtensions.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs index 9f0a665c3d..40ba5d533e 100644 --- a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs @@ -18,8 +18,9 @@ public static class IApplicationBuilderExtensions /// Adds necessary components such as routing to your application /// /// + /// /// - public static void UseJsonApi(this IApplicationBuilder app) + public static void UseJsonApi(this IApplicationBuilder app, Action AddUserMiddleware = null) { LogResourceGraphValidations(app); using (var scope = app.ApplicationServices.CreateScope()) @@ -31,6 +32,10 @@ public static void UseJsonApi(this IApplicationBuilder app) // An endpoint is selected and set on the HttpContext if a match is found app.UseRouting(); + // user defined middleware to run after routing occurs. + if (AddUserMiddleware != null) + AddUserMiddleware(app); + // middleware to run after routing occurs. app.UseMiddleware(); From c0c5fe64408e2b28f1edb3d880bea744f6569c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Tue, 21 Jan 2020 14:32:25 +0100 Subject: [PATCH 2/5] Add missing using --- .../Extensions/IApplicationBuilderExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs index 40ba5d533e..ef4e16946f 100644 --- a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs @@ -1,3 +1,4 @@ +using System; using JsonApiDotNetCore.Builders; using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Internal; From 9f13082092ba83d5cc8be030877119a22b3275a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Sat, 25 Jan 2020 12:34:04 +0100 Subject: [PATCH 3/5] Rename to insertExtraMiddleware --- .../Extensions/IApplicationBuilderExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs index ef4e16946f..5757f45c6c 100644 --- a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs @@ -19,9 +19,9 @@ public static class IApplicationBuilderExtensions /// Adds necessary components such as routing to your application /// /// - /// + /// Action intended for extra middleware that requires to be added after routing but before executing /// - public static void UseJsonApi(this IApplicationBuilder app, Action AddUserMiddleware = null) + public static void UseJsonApi(this IApplicationBuilder app, Action insertExtraMiddleware = null) { LogResourceGraphValidations(app); using (var scope = app.ApplicationServices.CreateScope()) @@ -34,8 +34,8 @@ public static void UseJsonApi(this IApplicationBuilder app, Action(); From 54ea0510e8b59e3895b493be5830143514d79b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Wed, 12 Feb 2020 10:34:21 +0100 Subject: [PATCH 4/5] Rework --- .../IApplicationBuilderExtensions.cs | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs index 5757f45c6c..08bb044275 100644 --- a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs @@ -1,4 +1,3 @@ -using System; using JsonApiDotNetCore.Builders; using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Internal; @@ -19,9 +18,11 @@ public static class IApplicationBuilderExtensions /// Adds necessary components such as routing to your application /// /// - /// Action intended for extra middleware that requires to be added after routing but before executing + /// Do not register any middleware + /// Register Authentication middleware + /// Register Authorization middleware /// - public static void UseJsonApi(this IApplicationBuilder app, Action insertExtraMiddleware = null) + public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMiddleware = false, bool useAuthentication = false, bool useAuthorization = false) { LogResourceGraphValidations(app); using (var scope = app.ApplicationServices.CreateScope()) @@ -30,18 +31,26 @@ public static void UseJsonApi(this IApplicationBuilder app, Action(); + if (useAuthorization) + { + app.UseAuthorization(); + }; - // Executes the endpoints that was selected by routing. - app.UseEndpoints(endpoints => endpoints.MapControllers()); + // middleware to run after routing occurs. + app.UseMiddleware(); + + // Executes the endpoints that was selected by routing. + app.UseEndpoints(endpoints => endpoints.MapControllers()); + } } /// From 7c8a0cbc6770a4b25f5ac0e18b6db73b65323d80 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 12 Feb 2020 10:38:53 -0800 Subject: [PATCH 5/5] docs: improved comments on usage UseJsonApi( ... ) --- .../IApplicationBuilderExtensions.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs index 08bb044275..8a5aad25bf 100644 --- a/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs @@ -15,13 +15,26 @@ 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. /// /// - /// Do not register any middleware - /// Register Authentication middleware - /// Register Authorization middleware - /// + /// 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);