Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use function switch to control routing access in ASP.NET Core

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容介绍了"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 EvaluateAsync(FeatureFilterEvaluationContext context) { var settings = context.Parameters.Get(); 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中如何使用功能开关控制路由访问"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report