diff --git a/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse.md b/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse.md index e5ca74f228..d3b08c63dc 100644 --- a/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse.md +++ b/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse.md @@ -1,24 +1,45 @@ --- title: Customize the behavior of AuthorizationMiddleware author: rick-anderson -ms.author: riande description: This article explains how to customize the result handling of AuthorizationMiddleware. +ms.author: riande monikerRange: '>= aspnetcore-5.0' +ms.date: 03/24/2022 +no-loc: ["Blazor Hybrid", Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] uid: security/authorization/authorizationmiddlewareresulthandler --- -# Customize the behavior of AuthorizationMiddleware +# Customize the behavior of `AuthorizationMiddleware` -Applications can register a `Microsoft.AspNetCore.Authorization.IAuthorizationMiddlewareResultHandler` to customize the way the middleware handles the authorization results. Applications can use the customized middleware to: +:::moniker range=">= aspnetcore-6.0" + +Apps can register an to customize how handles authorization results. Apps can use the `IAuthorizationMiddlewareResultHandler` to: * Return customized responses. * Enhance the default challenge or forbid responses. -The following code shows an example of an authorization handler that returns a custom response for certain kinds of authorization failures: +The following code shows an example implementation of `IAuthorizationMiddlewareResultHandler` that returns a custom response for specific authorization failures: -[!code-csharp[](customizingauthorizationmiddlewareresponse/sample/AuthorizationMiddlewareResultHandlerSample/MyAuthorizationMiddlewareResultHandler.cs)] +:::code language="csharp" source="customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/MyAuthorizationMiddlewareResultHandler.cs"::: Register `MyAuthorizationMiddlewareResultHandler` in `Startup.ConfigureServices`: -[!code-csharp[](customizingauthorizationmiddlewareresponse/sample/AuthorizationMiddlewareResultHandlerSample/Startup.cs?name=snippet)] +:::code language="csharp" source="customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/Startup.cs" id="snippet_ConfigureServices"::: - +:::moniker-end + +:::moniker range="< aspnetcore-6.0" + +Apps can register an to customize how handles authorization results. Apps can use the `IAuthorizationMiddlewareResultHandler` to: + +* Return customized responses. +* Enhance the default challenge or forbid responses. + +The following code shows an example implementation of `IAuthorizationMiddlewareResultHandler` that returns a custom response for specific authorization failures: + +:::code language="csharp" source="customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/MyAuthorizationMiddlewareResultHandler.cs"::: + +Register `MyAuthorizationMiddlewareResultHandler` in `Startup.ConfigureServices`: + +:::code language="csharp" source="customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/Startup.cs" id="snippet_ConfigureServices"::: + +:::moniker-end diff --git a/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/sample/AuthorizationMiddlewareResultHandlerSample/MyAuthorizationMiddlewareResultHandler.cs b/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/MyAuthorizationMiddlewareResultHandler.cs similarity index 100% rename from aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/sample/AuthorizationMiddlewareResultHandlerSample/MyAuthorizationMiddlewareResultHandler.cs rename to aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/MyAuthorizationMiddlewareResultHandler.cs diff --git a/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/sample/AuthorizationMiddlewareResultHandlerSample/Startup.cs b/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/Startup.cs similarity index 95% rename from aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/sample/AuthorizationMiddlewareResultHandlerSample/Startup.cs rename to aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/Startup.cs index e10674cd62..dc4b10fe89 100644 --- a/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/sample/AuthorizationMiddlewareResultHandlerSample/Startup.cs +++ b/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse/samples_snapshot/5.x/Startup.cs @@ -21,14 +21,14 @@ namespace AuthorizationMiddlewareResultHandlerSample public IConfiguration Configuration { get; } -#region snippet + // public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddSingleton(); } - #endregion + // // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)