简体   繁体   English

Azure云服务(经典)检索客户端IP地址

[英]Azure Cloud Service (Classic) retrieval of client IP address

I have read numerous posts, blogs, questions and answers on this so I am confident that it's not a simple standard solution. 我已经阅读了很多帖子,博客,问题和答案,所以我相信它不是一个简单的标准解决方案。

I am running an asmx style webservice within a web role and trying to retrieve the client IP address. 我正在web角色中运行asmx样式的webservice并尝试检索客户端IP地址。 I have multiple instances running which means the platform provides an external IP address and 'load balances' between the instances. 我有多个实例在运行,这意味着平台提供外部IP地址和实例之间的“负载平衡”。

This all works fine but the retrieval of the client IP address through simple means like request.UserHostAddress returns the external port of the deployment. 这一切都运行正常,但通过request.UserHostAddress等简单方法检索客户端IP地址返回部署的外部端口。

Other solutions like ServerVariables["HTTP_X_FORWARDED_FOR"] or ServerVariables["X_FORWARDED_FOR"] all return null and there is nothing in the Request.ServerVariables that carries the information. 其他解决方案,如ServerVariables["HTTP_X_FORWARDED_FOR"]ServerVariables["X_FORWARDED_FOR"]都返回null,并且Request.ServerVariables中没有任何内容包含信息。

The complete list of server variables available within a request are 请求中可用的服务器变量的完整列表是

HTTP_CONTENT_LENGTH:1584
HTTP_CONTENT_TYPE:text/xml; charset=utf-8
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_EXPECT:100-continue
HTTP_HOST:myhost
HTTP_SOAPACTION:myaction
HTTP_REQUEST_CONTEXT:appId=cid-###################################
HTTP_REQUEST_ID:|################################
LOCAL_ADDR: *This is the private instance ip address as expected*
REMOTE_ADDR: *This is the external port of the service*
REMOTE_HOST: *This is the external port of the service*

How can this be achieved ?? 怎么能实现?

Could you check ? 你能检查一下吗?

     var Request = HttpContext.Current.Request; 
     string IP = Request.Params["HTTP_CLIENT_IP"] ??
     Request.Headers["CF-CONNECTING-IP"].ToString() ??
     Request.Headers["Forwarded"] ??
     Request.UserHostAddress; 

Other approach for owin self host: owin self host的其他方法:

 if (Request.Properties.ContainsKey("MS_OwinContext"))
    {
    dynamic owinContext = Request.Properties["MS_OwinContext"];
    return owinContext.Request.RemoteIpAddress;
    }

Also it might be enabled in Azure server options https://totaluptime.com/kb/getting-the-original-client-ip-with-x-forwarded-for-in-your-code/ 此外,它可能在Azure服务器选项中启用https://totaluptime.com/kb/getting-the-original-client-ip-with-x-forwarded-for-in-your-code/

Thanks to anyone that looked at this and to Oleg for his attempts. 感谢任何看过这个的人和奥列格的尝试。 It took a lot of digging around with remote debuggers running into an Azure deployment but eventually I found the issue. 使用远程调试器进行Azure部署需要花费大量时间,但最终我发现了这个问题。

We have an old style .asmx webservice with a WCF service in front of it with various 'inspectors' and 'routers'. 我们有一个旧式的.asmx webservice,前面有一个WCF服务,有各种“检查员”和“路由器”。 It's all pretty ugly but has worked for a long while (as I am sure many systems do!). 这一切都很丑陋,但已经工作了很长时间(我相信很多系统都会这样做!)。 The logging of the remote client address has been broken for some time but the need to have it working again has become more important again recently. 远程客户端地址的记录已经打破了一段时间,但最近再次使用它的需求变得更加重要。

The issue is related to the WCF stack behaviors and how they deal with HTTP headers as opposed to SOAP headers. 该问题与WCF堆栈行为以及它们如何处理HTTP头而不是SOAP头相关。 We had a version change in the Soap specification which broke the system a while back but that was fixed by setting SoapProcessingEnabled on a routing behavior to true which in essence strips all http headers and allows incompatible Soap services to communicate. 我们在Soap规范中有一个版本更改,它在一段时间内破坏了系统,但是通过将路由行为上的SoapProcessingEnabled设置为true来修复,这实际上剥离了所有http头并允许不兼容的Soap服务进行通信。

My attempts to set the header specifically in another behavior failed due to an order execution problem. 由于订单执行问题,我尝试在另一个行为中专门设置标头失败。

Looking forward to dropping the whole asmx / wcf stack and starting again but can't quite justify the months of development time ! 期待放弃整个asmx / wcf堆栈并重新开始,但不能完全证明开发时间的几个月! Thanks again for the help 再次感谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM