c# - apiprotector.net 错误 已添加具有相同密钥的项目。密钥:X-API-保护器
问题描述
ApiProtector.Net ( https://apiprotector.net/ ),给我错误:已添加具有相同键的元素。关键:X-API-Protector,当我使用多个规则保护端点时。
[ApiProtector(ApiProtectionType.ByIpAddress, Limit: 10, TimeWindowSeconds: 10, PenaltySeconds: 60)]
[ApiProtector(ApiProtectionType.ByIdentity, Limit: 10, TimeWindowSeconds: 10, PenaltySeconds: 60)]
解决方案
我发现解决方案是 ApiProtector.Net 中的一个错误。尝试在 hader 中多次添加密钥:X-API-Protector
创建一个继承自 ApiProtector 的类,并覆盖 OnActionExecuted 方法。
public class CustomApiProtector : ApiProtector
{
public CustomApiProtector(ApiProtectionType type, uint limit, uint timeWindowSeconds, uint penaltySeconds ):base(type, limit, timeWindowsSecond, penaltySeconds)
{
}
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
IHeaderDictionary headers = context.HttpContext.Response.Headers;
StringValues locationHeaderValue = string.Empty;
if (headers.TryGetValue("X-API-Protector", out locationHeaderValue))
{
context.HttpContext.Response.Headers.Remove("X-API-Protector");
}
}
}
推荐阅读
- pycharm - pycharm 不显示代码建议或颜色 python 代码
- css - React 中表格行的样式
- linux - 如何使用 grep 和正则表达式仅显示 3 个字符的名称
- javascript - 使用 Firebase 部署 Nuxt 站点地图
- sql - 如果它们以数字命名,如何访问列
- macos - Mac 64 位中的多处理运行时错误 freeze_support()
- tensorflow - 在 Keras 中向 BiLSTM 模型添加 CRF 层(2021 年 1 月)
- aws-lambda - 用存根客户端覆盖 boto3 客户端
- asp.net - 添加带有单词扩展名的脚注
- typescript - 使用 Record 类型初始化打字稿中的空对象