美文网首页.NETAsp.net开发WebApi
DotNet获取客户端的IP地址

DotNet获取客户端的IP地址

作者: CoderMiner | 来源:发表于2018-07-31 16:55 被阅读34次

    DotNet获取客户端的IP地址

    image
    public static string GetClientIpAddress(HttpRequestMessage request)
    {
        if (request.Properties.ContainsKey(HttpContext))
        {
            dynamic ctx = request.Properties[HttpContext];
            if (ctx != null)
            {
                return ctx.Request.UserHostAddress;
            }
        }
        if (request.Properties.ContainsKey(RemoteEndpointMessage))
        {
            dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
            if (remoteEndpoint != null)
            {
                return remoteEndpoint.Address;
            }
        }
        if (request.Properties.ContainsKey(OwinContext))
        {
            dynamic ctx = request.Properties[OwinContext];
            if (ctx != null)
            {
                return ctx.Request.RemoteIpAddress;
            }
        }
        return null;
    }
    

    原文连接

    相关文章

      网友评论

        本文标题:DotNet获取客户端的IP地址

        本文链接:https://www.haomeiwen.com/subject/xevhvftx.html