背景
在工作中遇到一个与第三方服务商进行API对接的项目,需要进行IP白名单处理,于是我立马想到使用中间件做IP过滤,在此记录一下
添加中间件
- 新建一个SafeIpList类
public class SafeIpList
{
public string ip_list_name { get; set; }
}
- 在配置文件中配置IP白名单
"SafeIpList": {
"ip_list_name ": "127.0.0.1"
}
- 读取注入配置文件中的IP list
services.Configure<SafeIpList>(Configuration.GetSection("SafeIpList"));
- 新建一个中间件写入以下代码
public class SafeListMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<SafeListMiddleware> _logger;
private SafeIpList _SafeIpList = null;
public SafeListMiddleware(
RequestDelegate next,
ILogger<SafeListMiddleware> logger,
IOptionsMonitor<SafeIpList> options)

本文记录了一个使用.Net5中间件进行IP过滤的实践过程,主要步骤包括新建SafeIpList类,配置IP白名单,读取配置文件中的IP列表,编写中间件代码,并启用中间件来实现功能。
最低0.47元/天 解锁文章
630

被折叠的 条评论
为什么被折叠?



