自定义 AuthorizationMiddleware
的行为
应用可以注册 IAuthorizationMiddlewareResultHandler,以自定义 AuthorizationMiddleware 处理授权结果的方式。 应用可将 IAuthorizationMiddlewareResultHandler
用于:
- 返回自定义的响应。
- 增强默认质询或禁止响应。
以下代码显示了 IAuthorizationMiddlewareResultHandler
的示例实现,它会针对特定授权失败返回自定义响应:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
public class SampleAuthorizationMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler
{
private readonly AuthorizationMiddlewareResultHandler defaultHandler = new();
public async Task HandleAsync(
RequestDelegate next,
HttpContext context,
AuthorizationPolicy policy,
PolicyAuthorizationResult authorizeResult)
{
// If the authorization was forbidden and the resource had a specific requirement,
// provide a custom 404 response.
if (authorizeResult.Forbidden
&& authorizeResult.AuthorizationFailure!.FailedRequirements
.OfType<Show404Requirement>().Any())
{
// Return a 404 to make it appear as if the resource doesn't exist.
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
// Fall back to the default implementation.
await defaultHandler.HandleAsync(next, context, policy, authorizeResult);
}
}
public class Show404Requirement : IAuthorizationRequirement { }
在 Program.cs
中注册 IAuthorizationMiddlewareResultHandler
的实现:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<
IAuthorizationMiddlewareResultHandler, SampleAuthorizationMiddlewareResultHandler>();
var app = builder.Build();
应用可以注册 IAuthorizationMiddlewareResultHandler,以自定义 AuthorizationMiddleware 处理授权结果的方式。 应用可以将 IAuthorizationMiddlewareResultHandler
用于:
- 返回自定义的响应。
- 增强默认质询或禁止响应。
以下代码显示了 IAuthorizationMiddlewareResultHandler
的示例实现,它会针对特定授权失败返回自定义响应:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Http;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
public class MyAuthorizationMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler
{
private readonly AuthorizationMiddlewareResultHandler
DefaultHandler = new AuthorizationMiddlewareResultHandler();
public async Task HandleAsync(
RequestDelegate requestDelegate,
HttpContext httpContext,
AuthorizationPolicy authorizationPolicy,
PolicyAuthorizationResult policyAuthorizationResult)
{
// if the authorization was forbidden and the resource had specific requirements,
// provide a custom response.
if (Show404ForForbiddenResult(policyAuthorizationResult))
{
// Return a 404 to make it appear as if the resource does not exist.
httpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
return;
}
// Fallback to the default implementation.
await DefaultHandler.HandleAsync(requestDelegate, httpContext, authorizationPolicy,
policyAuthorizationResult);
}
bool Show404ForForbiddenResult(PolicyAuthorizationResult policyAuthorizationResult)
{
return policyAuthorizationResult.Forbidden &&
policyAuthorizationResult.AuthorizationFailure.FailedRequirements.OfType<
Show404Requirement>().Any();
}
}
public class Show404Requirement : IAuthorizationRequirement { }
在 Startup.ConfigureServices
中注册 MyAuthorizationMiddlewareResultHandler
:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddSingleton<IAuthorizationMiddlewareResultHandler,
MyAuthorizationMiddlewareResultHandler>();
}