我正在开发Angular应用程序并在后端使用Asp .Net Core。我想知道在Controller层运行浏览器的客户端机器的IP。因此,当请求从浏览器转到后端时,它将通过下面的代理文件进行路由。
{
"/shell/api": {
"target": "http://<server-name>:1000",
"secure": false,
"pathRewrite": {
"^/shell": ""
},
}
}
从角度服务,我这样称呼服务器。
loadData() {
return this._http.get('shell/api/get/data')
.map(res => res.json());
}
在Controller,我期望在下面的对象中的IP地址。
HttpContext.Connection.RemoteIpAddress
现在,浏览器发送请求,角接收它,通过代理发送给Controller。由于代理,我在Controller中获取服务器IP地址。
有没有办法在这种情况下获得客户端IP?
也许您可以为服务器的每个请求添加客户端IP。
如果要在UI中获取客户端IP,可以使用以下代码。
const findIP = new Promise(r => {
const w = window;
const a = new (w['RTCPeerConnection'] || w['mozRTCPeerConnection'] || w['webkitRTCPeerConnection'])({ iceServers: [] });
const b = () => { };
a.createDataChannel('');
a.createOffer(c => a.setLocalDescription(c, b, b), b);
a.onicecandidate = c => {
try {
console.log(c);
if (c.candidate) {
c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)
}
} catch (e) {
console.log(e);
}
};
}
);
findIP.then(ip => {
this.ip = ip;
}).catch(e => console.error(e));