如何在WebMethod中获取调用者的IP地址?
如何在WebMethod中获取调用者的IP地址?
[WebMethod] public void Foo() { // HttpRequest... ? - Not giving me any options through intellisense... } 使用C#和ASP.NET
HttpContext.Current.Request.UserHostAddress是你想要的。
只是谨慎。 IP地址不能用来唯一标识客户端。 NAT防火墙和企业代理无处不在,隐藏了许多用户在一个IP之后。
尝试:
 Context.Request.UserHostAddress 
尝试这个:
 string ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
没有在webMethod中尝试过,但是我在标准的HttpRequests中使用它
  HttpContext实际上在WebService基类中是可用的,所以只需使用Context.Request (或者也指向当前上下文的HttpContext.Current )来访问由HttpRequest提供的成员。 
我做了以下function:
 static public string sGetIP() { try { string functionReturnValue = null; String oRequestHttp = WebOperationContext.Current.IncomingRequest.Headers["User-Host-Address"]; if (string.IsNullOrEmpty(oRequestHttp)) { OperationContext context = OperationContext.Current; MessageProperties prop = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; oRequestHttp = endpoint.Address; } return functionReturnValue; } catch (Exception ex) { return "unknown IP"; } } 
这项工作只在Intranet上,如果你有一些代理服务器或者你应该研究,如果原始IP被移动到http数据包的其他地方。
- 你如何将ASP.Net MVC融入现有的网站项目?
- 链接button属性在新标签中打开?
- 我如何使用global.asax中的Server.MapPath()?
- 路由请求时,HttpContext.Current.Session为空
- 在ASP.NET应用程序之间传递会话数据
- 如何在ASP.NET Web窗体中使用dependency injection
- 如何在Visual Studio中debugging单个线程?
- aspnet_compiler发现System.Web.WebPages 1.0.0.0的错误版本,而不是2.0.0.0
- 使用ConfigurationManager从任意位置加载configuration