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

The method of using function switch to Control Route access in ASP.NET Core

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

Share

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

Today, I would like to share with you the relevant knowledge of ASP.NET Core using function switches to control routing access. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. Function switch

Function switch (Feature flags) is such a deployment technology that helps to improve the flexibility of applications.

Using function switches, you can deploy new features to a production environment, but limit their availability.

By using the switch, you can activate a new feature to control whether or not to restart the application or deploy new code.

They separate the release of new features from code deployment.

3. Realize

First, we need to reference the nuget package Microsoft.FeatureManagement.AspNetCore.

Then, modify the Startup.cs code:

Public void ConfigureServices (IServiceCollection services) {... Services.AddFeatureManagement ();...}

Next, add a configuration:

FeatureManagement: {"ForbiddenDebugEndpoint": false} 3. Use

Modify the DebugMiddleware we implemented last time:

Public class DebugMiddleware: IMiddleware {private readonly IFeatureManager _ featureManager; public DebugMiddleware (IFeatureManager featureManager) {_ featureManager = featureManager;} public async Task InvokeAsync (HttpContext context, RequestDelegate next) {var isDebugEndpoint = context.Request.Path.Value.Contains ("/ test"); var debugEndpoint = await _ featureManager.IsEnabledAsync ("ForbiddenDebugEndpoint") If (isDebugEndpoint & & debugEndpoint) {context.SetEndpoint (new Endpoint ((context) = > {context.Response.StatusCode = StatusCodes.Status403Forbidden; return Task.CompletedTask;}, EndpointMetadataCollection.Empty, "No access");} await next (context) }}

The key is this sentence, we used the function switch:

Var debugEndpoint = await _ featureManager.IsEnabledAsync ("ForbiddenDebugEndpoint"). Conclusion:

After running, we found that we only need to modify the configuration without restarting the program, and we can control whether the route can be accessed:

These are all the contents of the article "how ASP.NET Core uses function switches to control routing access". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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