如何在单向ASMX Web服务中获取客户端IP地址
我有一个配置为一种方式的Web服务.或者,SoapDocumentMethod属性具有一个称为"OneWay"的属性.设置为true.
I have a web service that is configured as one way. Or rather, the SoapDocumentMethod attribute has a property that is called "OneWay". That is set to true.
我正在尝试获取客户端请求的IP地址.我得到的事实是我无法获得确切的IP地址,因为客户端可能落后于其他网络概念.没关系我想要一个在服务上发起呼叫的人的IP地址,因此,如果那台前端服务器位于某些客户端计算机的顶部,就这样吧.
I'm trying to get the IP address of the client request. I get the fact that I won't get the exact IP address because the client may be behind other networking concepts. That's irrelevant. I want an IP address of whoever initiated the call on the service, so if that's some front end server sitting on top of some clients machine, so be it.
我尝试使用HttpContext,但这似乎不起作用.如果我在运行Web服务的同一台计算机上访问我的Web服务,则将填充HttpContext并获取IP地址.但是,我的Web服务和HttpContext的任何外部使用都有些混乱.尝试查找有关请求的信息时,它会在许多属性上引发异常.因此我无法获得客户端IP地址.如果启用服务跟踪,则它将开始工作.但这只是暂时的解决方法.这不是可接受的解决方案.
I tried using HttpContext, but that doesn't seem to work. If I hit my web service on the same machine as the web service is running, then HttpContext is populated and I can get the IP address. However, any external use of my web service and the HttpContext is a little messed up. It throws exceptions on many of the properties when trying to find out information on the request. So I cannot get the client IP address. If you enable tracing of the service, It then starts working. But that's only a temporary work around. It's not an acceptable solution.
那么..当您拥有单向Web服务时,获取客户端IP地址的最佳方法是什么?
So.. what's the best way to get the client IP address when you have a one way web service?
I've already looked here: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapdocumentmethodattribute.oneway.aspx
幸运的是,您有一些选择.
Luckily, you have some options.
1)将IP地址作为参数传递->不推荐. 2)为IP地址创建一个SOAP标头,并从客户端传递它
1) Pass the IP address as a parameter --> NOT recommended. 2) Create a SOAP header for the IP Address and pass it from the client
我为您提供了选项2的快速示例.
I wrote you a quick sample for option 2.
首先,您需要扩展SoapHeader并创建一个类来传递IP地址.
First, you need to extend SoapHeader and create a class to pass the IP Address.
using System.Web.Services.Protocols;
namespace SoapHeaders
{
public class IpAddressHeader : SoapHeader
{
public string IpAddress;
}
}
接下来,您需要使用该标头定义一个Web服务以及使用该标头的Web方法.
Next, you need to define a web service with that header, and a web method that uses it.
using System;
using System.IO;
using System.Web.Services.Protocols;
using System.Web.Services;
using SoapHeaders;
namespace WebService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class SoapHeaderService : System.Web.Services.WebService
{
public IpAddressHeader IpAddressHeader;
[WebMethod]
[SoapDocumentMethod(OneWay = true)]
[SoapHeader("IpAddressHeader")]
public void LogIpAddress()
{
var logFile = string.Format(@"C:\Logs\{0:yyyy-MM-dd HH.mm.ss.ffff}.log", DateTime.Now);
string ipAddress;
if (IpAddressHeader == null || string.IsNullOrEmpty(IpAddressHeader.IpAddress))
{
ipAddress = "?";
}
else
{
ipAddress = IpAddressHeader.IpAddress;
}
File.WriteAllText(logFile, string.Format("Client Address --> {0}", ipAddress));
}
}
}
最后,您需要一个客户端来使用该服务并传递标头信息.我刚刚制作了一个快速的控制台应用程序,但实际上可以是任何东西:
Finally, you need a client to consume the service and pass the header information. I just made a quick console application, but it could be anything, really:
using System.Net;
using ConsoleApplication.soapHeaderServices;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var ipAddressHeader = new IpAddressHeader();
ipAddressHeader.IpAddress = GetIpAddress();
var serviceClient = new SoapHeaderService();
serviceClient.IpAddressHeaderValue = ipAddressHeader;
serviceClient.LogIpAddress();
}
static string GetIpAddress()
{
var ipAddress = "?";
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var address in host.AddressList)
{
if (address.AddressFamily.ToString() != "InterNetwork")
{
continue;
}
ipAddress = address.ToString();
break;
}
return ipAddress;
}
}
}
我在远程测试服务上部署了该服务,并从我的PC对其进行了调用.工作正常.创建的日志文件的内容:客户端地址-> 192.168.1.41
I deployed the service on a remote test service and called it from my PC. Works fine. Contents of the log file created: Client Address --> 192.168.1.41
希望您能找到帮助!
编辑:请勿使用我的下方代码.这是错误的.我将其作为不执行操作的示例. 服务器变量在单向操作中不可用.
DO NOT USE MY BELOW CODE. It is wrong. I'm leaving it as an example of what NOT to do. Server variables are not available in one-way operations.
原始答案.请注意使用:请参阅我在EDIT上方的最新答案.
ORIGINAL ANSWER. DO NOTE USE: See my updated answer above the EDIT.
我只是在新的ASP.Net Web服务应用程序上进行了烟熏测试.在我可以访问的每个端点上工作.
I just smoke tested this on a new ASP.Net Web Service Application. Works from every endpoint I have access to.
HttpRequest.UserHostAddress是获取客户端IP地址的文档方法.如果那对您不起作用,我将寻找根本原因.
HttpRequest.UserHostAddress is the document way to get the client's IP address. If that is not working for you, I would look for underlying causes.
[WebMethod]
[SoapDocumentMethod(OneWay = true)]
public void Silent()
{
var address = HttpContext.Current.Request.UserHostAddress;
// Do something with address
Trace.Write(address);
}