Prepare IAuthorizationMiddlewareResultHandler for 6.0 (#25412)
parent
5f772cf94d
commit
c10148abcd
|
@ -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 <xref:Microsoft.AspNetCore.Authorization.IAuthorizationMiddlewareResultHandler> to customize how <xref:Microsoft.AspNetCore.Authorization.AuthorizationMiddleware> 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":::
|
||||
|
||||
<!-- <xref:Microsoft.AspNetCore.Authorization.IAuthorizationMiddlewareResultHandler /> -->
|
||||
:::moniker-end
|
||||
|
||||
:::moniker range="< aspnetcore-6.0"
|
||||
|
||||
Apps can register an <xref:Microsoft.AspNetCore.Authorization.IAuthorizationMiddlewareResultHandler> to customize how <xref:Microsoft.AspNetCore.Authorization.AuthorizationMiddleware> 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
|
||||
|
|
|
@ -21,14 +21,14 @@ namespace AuthorizationMiddlewareResultHandlerSample
|
|||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
#region snippet
|
||||
// <snippet_ConfigureServices>
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddRazorPages();
|
||||
services.AddSingleton<IAuthorizationMiddlewareResultHandler,
|
||||
MyAuthorizationMiddlewareResultHandler>();
|
||||
}
|
||||
#endregion
|
||||
// </snippet_ConfigureServices>
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
Loading…
Reference in New Issue