【问题标题】:Send a HTTP request from another IP address in .NET Core从 .NET Core 中的另一个 IP 地址发送 HTTP 请求
【发布时间】:2020-10-08 08:50:21
【问题描述】:

上下文

我目前正在开发一个 .net Core 3.1 API,该 API 具有一种身份验证方法,用于检查 HTTP 请求是否是从特定 IP 地址发送的。 请求者的 IP 地址应与存储在数据库或本地主机中的 IP 地址相匹配,否则客户端将被拒绝。

代码

我有以下代码:

控制器

 public async Task<IActionResult> AuthenticatePlanbord([FromBody] AuthPlanbordRequest request)
        {
            if (request.AuthType == AuthType.Planbord)
            {
                // Validate the IP address of the client to check if the request is made from the server of the planbord.
                var ip = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
                var AuthResponse = await _authService.AuthenticatePlanbordAsync(ip, request.DatabaseName, request.UserId);
                if (AuthResponse == null) return Unauthorized(new ServerResponse(false, "Unauthorized", HttpStatusCode.Unauthorized));
                return Ok(new ServerResponse(TokenGenerator.GenerateJsonWebToken(AuthResponse)));
            }
            return BadRequest(new ServerResponse(false, _localizer["AuthTypeNotSupported"], HttpStatusCode.BadRequest));
        }

身份验证服务

public async Task<AuthEntityPlanbord> AuthenticatePlanbordAsync(string ip, string databaseName, Guid userId = default)
        {
            _unitOfWork.Init();
            // Check if the request does not originate from localhost
            if (ip != "::1")
            {
                var Ip = await _unitOfWork.Connection.ExecuteScalarAsync<string>("SELECT IpAdres FROM PlanbordAutorisaties WITH(NOLOCK) WHERE IpAdres = @Ip ", new { Ip = ip }, _unitOfWork.Transaction);
                if (string.IsNullOrEmpty(Ip)) return null;
            }
            var userData = await _unitOfWork.AuthRepository.AuthenticatePlanbordAsync(userId);
            userData.IPAdress = ip;
            userData.DatabaseName = databaseName;
            return userData;
        }

问题和疑问

为了全面测试逻辑是否有效,我想编写一个集成测试,它从与 localhost 不同的 IP 地址发送 HTTP 请求。这在.net Core中可能吗?还是应该只依赖单元测试?

【问题讨论】:

  • 可以使用任何支持使用代理服务器进行 Web 请求的编程语言的代理服务器。不过,我不确定用实际请求对其进行集成测试是否有利。
  • 感谢@John 的快速回复,然后我会调查代理服务器。但我认为发送实际的 HTTP 请求将有利于测试。

标签: c# http .net-core ip


【解决方案1】:

简单的方法(适用于任何语言)是使用reqbin 或类似服务来模拟请求。作为一项在线服务,它将拥有不同的 IP。

您可以找到其他类似的服务。 这个特别有一个可用的 API 示例。因此,如果你想将它集成到你的单元测试或类似的东西中,你只需要模拟一个对其 api 的 POST 请求,参数指向你的端点,这样你就可以模拟来自未列入白名单的 IP 的外部请求。

【讨论】:

  • 感谢@Liquid Core 的快速回复。但我担心这个解决方案不适用于我的情况,因为我想在本地托管的 API 构建上运行我的集成测试,而reqbin 将无法到达我的端点。但是这个解决方案可以在 CI/CD 构建服务器或 IIS 测试服务中工作。但我工作的公司没有这些。
  • @JoepVerhoeven 也许是这个? stackoverflow.com/questions/39689858/…
  • 我会调查的。感谢您的建议!
  • @JoepVerhoeven 好的,如果它解决了标记答案,否则回到这里,我会尝试找到更多
  • 好吧,您的解决方案确实不错,但仍然无法回答问题;是否可以从具有不同发件人 IP 地址的 .net Core 应用程序发送 HTTP 请求?但我非常感谢您的帮助。
【解决方案2】:

是的,如果这是一个 API 调用,您可以使用 HTTP 客户端创建请求。

您可以将 HTTP 客户端设置为在处理程序中使用代理。

var httpclienthandler = new HttpClientHandler
            {
                // Set creds in here too if your proxy needs auth
                Proxy = new WebProxy("proxyIp")
            };
var httpClient = new HttpClient(httpclienthandler);

如果这是一个网站操作,您可以将浏览器设置为使用代理,或者直接连接到 VPN?

【讨论】:

  • 他不能使用任何外部服务。他必须在当地做。
猜你喜欢
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 2015-01-10
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多