4

ASP.NET Core使用功能开关控制路由访问(续)#yyds干货盘点#

 2 years ago
source link: https://blog.51cto.com/MyIO/5019911
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ASP.NET Core使用功能开关控制路由访问(续)#yyds干货盘点#

原创

My空格IO 2022-02-18 12:45:35 博主文章分类:ASP.NET Core ©著作权

文章标签 lua 自定义 公众号 文章分类 .Net 编程语言 阅读数1541

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

但其实我们使用了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使用功能开关控制路由访问(续)#yyds干货盘点#_公众号

想了解更多内容,请关注我的个人公众号”My IO“​

ASP.NET Core使用功能开关控制路由访问(续)#yyds干货盘点#_自定义_02

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK