javascript - 在 IIS 中运行的 Blazor 服务器端应用程序中获取远程 IP 地址不起作用?
问题描述
我需要获取客户端的IP地址,所以我做了以下事情:
创建了一个控制器并添加了这个动作:
[HttpGet("[action]")] public ActionResult IpAddress() { var remoteIpAddress = HttpContext.Connection.RemoteIpAddress; if (remoteIpAddress != null) return Ok(remoteIpAddress.ToString()); return new EmptyResult(); }
将以下脚本添加到我的
site.js
:window.getIpAddress = () => { return fetch('/api/utils/ipaddress') .then((response) => response.text()) .then((data) => { return data }) }
也设置
Startup.cs
到用户控制器,还添加以下内容:app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
最后,将以下内容添加到我的剃须刀组件中:
var ipAddress = await jsRuntime.InvokeAsync<string>("getIpAddress").ConfigureAwait(true);
然后我发布到 IIS 并访问该应用程序。
但是,IP地址不是客户端的IP地址(实际上是标准网关的值),而是运行IIS的机器的IP地址!
我究竟做错了什么?
解决方案
推荐阅读
- javascript - 使用 Konva 向压缩圆可视化添加缩放(从中心缩放和重新定位)
- c# - 向记录添加数字以避免 HashSet 中的重复值
- c++ - 强制失败通用模板但允许专业化,加上“方法存在调度”
- swift - 使用 Alamofire 访问 Localhost API
- devops - 当工作项在 Devops 中更改为完成时,如何创建共享点列表项?
- reactjs - 如何在使用 getStaticPaths 的类别中使用嵌套的 getStaticPaths 制作帖子文件?(使用 API)
- javascript - 如何使用node js(mongoose)删除mongodb中的相关架构
- angular - 如何在 SharePoint 之外获取/发布 SharePoint 列表/库数据?
- mysql - mysql v0.2.37 TypeORM 实体迁移:生成 DROP AND ADD where MODIFY would do
- javascript - Vue.js 组件注册模板