好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

ASP.NET Core使用功能开关控制路由访问操作(续)

前言:

在 前面的文章 ,我们介绍了? ?使用功能开关控制路由访问??。

但其实我们使用了2个条件做的判断:

var isDebugEndpoint = context.Request.Path.Value.Contains("/test");
var debugEndpoint = await _featureManager.IsEnabledAsync("ForbiddenDebugEndpoint");

if (isDebugEndpoint && debugEndpoint)

如果仅用功能开关来控制:

var debugEndpoint = await _featureManager.IsEnabledAsync("ForbiddenDebugEndpoint");

if (debugEndpoint)

这样是不是更符合功能开关的含义呢!

一、IFeatureFilter介绍

IFeatureFilter (功能过滤器)可用于确定是否满足某些条件以启用一项功能。

功能过滤器可以自由使用任何可用的标准,例如流程状态或请求内容。

可以为给定功能注册功能过滤器,如果任何特征过滤器评估为真,该特征将被考虑启用。

??在本文,我们可以判断当前路由地址是否为调试地址,让评估返回真。??

二、实现

自定义功能过滤器实现代码如下:

public class DebugFeatureSettings
{
? ? public string[] DebugEndpoints { get; set; }
}

[FilterAlias("DebugFeatureFilter")]
public class DebugFeatureFilter : IFeatureFilter
{
? ? private readonly IHttpContextAccessor _httpContextAccessor;
? ? public DebugFeatureFilter(IHttpContextAccessor httpContextAccessor)
? ? {
? ? ? ? _httpContextAccessor = httpContextAccessor;
? ? }
? ? public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context)
? ? {
? ? ? ? var settings = context.Parameters.Get<DebugFeatureSettings>();
? ? ? ? foreach (var endPoint in settings.DebugEndpoints)
? ? ? ? {
? ? ? ? ? ? var isDebugEndpoint = _httpContextAccessor.HttpContext.Request.Path.Value.Contains(endPoint);
? ? ? ? ? ? return Task.FromResult(isDebugEndpoint);
? ? ? ? }
? ? ? ? return Task.FromResult(false);
? ? }
}

我们注入了?? IHttpContextAccessor ???,用于获取当前请求上下文,然后判断当前路由地址是否包含?? DebugEndpoints ??配置的值。

三、使用

修改我们上次实现的??DebugMiddleware??:

public class DebugMiddleware : IMiddleware
{
? ? private readonly IFeatureManager _featureManager;

? ? public DebugMiddleware(IFeatureManager featureManager)
? ? {
? ? ? ? _featureManager = featureManager;
? ? }
? ? public async Task InvokeAsync(HttpContext context, RequestDelegate next)
? ? {
? ? ? ? var debugEndpoint = await _featureManager.IsEnabledAsync("ForbiddenDebugEndpoint");

? ? ? ? if (debugEndpoint)
? ? ? ? {
? ? ? ? ? ? context.SetEndpoint(new Endpoint((context) =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? context.Response.StatusCode = StatusCodes.Status403Forbidden;
? ? ? ? ? ? ? ? return Task.CompletedTask;
? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? ? EndpointMetadataCollection.Empty,
? ? ? ? ? ? ? ? ? ? ? ? "无权访问"));
? ? ? ? }

? ? ? ? await next(context);
? ? ? ? ? ??
? ? }
}

然后将配置修改为如下形式:

"FeatureManagement": {
? ? "ForbiddenDebugEndpoint": {
? ? ? ? "EnabledFor": [
? ? ? ? {
? ? ? ? ? ? "Name": "DebugFeatureFilter",
? ? ? ? ? ? "Parameters": {
? ? ? ? ? ? ? ? "DebugEndpoints": [ "/test" ]
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ]
? ? }
}

结论:

运行后我们发现,只有符合功能开关设置的路由地址才会被限制访问:

到此这篇关于ASP.NET Core使用功能开关控制路由访问操作(续)的文章就介绍到这了,更多相关ASP.NET Core使用功能开关控制路由访问内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

查看更多关于ASP.NET Core使用功能开关控制路由访问操作(续)的详细内容...

  阅读:42次