webapi获取请求地址的IP

摘要:
usingSystem.Net.Http;publicstaticclassHttpRequestMessageExtensions{privateconststringHttpContext="MS_HttpContext";privateconststringRemoteEndpointMessage="System.ServiceModel.Channels.RemoteEndpointMe
using System.Net.Http;
public static class HttpRequestMessageExtensions
{
    private const string HttpContext = "MS_HttpContext";
    private const string RemoteEndpointMessage =
        "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
    private const string OwinContext = "MS_OwinContext";
    public static string GetClientIpAddress(this HttpRequestMessage request)
    {
       // Web-hosting. Needs reference to System.Web.dll
       if (request.Properties.ContainsKey(HttpContext))
       {
           dynamic ctx = request.Properties[HttpContext];
           if (ctx != null)
           {
               return ctx.Request.UserHostAddress;
           }
       }
       // Self-hosting. Needs reference to System.ServiceModel.dll. 
       if (request.Properties.ContainsKey(RemoteEndpointMessage))
       {
            dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
            if (remoteEndpoint != null)
            {
                return remoteEndpoint.Address;
            }
        }
       // Self-hosting using Owin. Needs reference to Microsoft.Owin.dll. 
       if (request.Properties.ContainsKey(OwinContext))
       {
           dynamic owinContext = request.Properties[OwinContext];
           if (owinContext != null)
           {
               return owinContext.Request.RemoteIpAddress;
           }
       }
        return null;
    }
}

References required:

  • HttpContextWrapper - System.Web.dll
  • RemoteEndpointMessageProperty - System.ServiceModel.dll
  • OwinContext - Microsoft.Owin.dll (you will have it already if you use Owin package)

第二种:
((System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;

免责声明:文章转载自《webapi获取请求地址的IP》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇HTML统计图表DataView筛选出最新的十条数据的方法总结;下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Shiro权限管理框架(四):深入分析Shiro中的Session管理

其实关于Shiro的一些学习笔记很早就该写了,因为懒癌和拖延症晚期一直没有落实,直到今天公司的一个项目碰到了在集群环境的单点登录频繁掉线的问题,为了解决这个问题,Shiro相关的文档和教程没少翻。最后问题解决了,但我觉得我也是时候来做一波Shiro学习笔记了。 本篇是Shiro系列第四篇,Shiro中的过滤器初始化流程和实现原理。Shiro基于URL的权限...

Protobuf 语言指南(proto3)

Protobuf 语言指南(proto3)Protocol Buffer是Google的语言中立的,平台中立的,可扩展机制的,用于序列化结构化数据 - 对比XML,但更小,更快,更简单。您可以定义数据的结构化,然后可以使用特殊生成的源代码轻松地在各种数据流中使用各种语言编写和读取结构化数据。 定义消息类型 先来看一个非常简单的例子。假设你想定义一个“搜索...

利用Elasticsearch搭建全球域名解析记录

前言 数据来源,由Rapid7收集并提供下载https://scans.io/study/sonar.fdns 下载Elasticsearch 2.3 ElasticSearch是一个基于Lucene开发的搜索服务器,具有分布式多用户的能力,ElasticSearch是用Java开发的开源项目(Apache许可条款),基于Restful Web接口,能够...

C# Dictionary.Keys用法及代码示例

此属性用于获取包含Dictionary中的键的集合。 用法: public System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection Keys { get; } 返回值:它返回一个包含Dictionary中关键字的集合。 以下示例程序旨在说明上面讨论的属性的使用:...

使用Xftp&amp;amp;Xshell 工具进行文件上传与运行webapi

前言: 由于我们平时在开发微服务的时候,都是在windows 或者mac上开发的,开完完成一些功能,需要发布到服务器上进行提测。今天,我主要介绍两个工具来说明一下如何上传以及把程序跑起来。 正文: 准备工作: 1、Xftp 6工具 ,下载地址:https://www.netsarang.com/zh/xftp/ 2、Xshell 6 工具,下载地址:htt...

FTP操作(FTPClient)

//FTP开源封装的类 using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP {     /// <summary>     /// FTP客户端操作类     /// </summary>  ...