• c# 怎么获取自己的IP地址


    1、aspx页面,asp.net项目的页面

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ip.aspx.cs" Inherits="MvcAppTest.ip" %>
    <script language="c#" runat="server">
        /// 获取客户端IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIPAddress()
        {
            string userIP = "未获取用户IP";  
    
               try  
               {  
                  if (System.Web.HttpContext.Current == null  
              || System.Web.HttpContext.Current.Request == null  
               || System.Web.HttpContext.Current.Request.ServerVariables == null)  
                       return "";  
      
                   string CustomerIP = "";  
      
                   //CDN加速后取到的IP   
                   CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];  
                   if (!string.IsNullOrEmpty(CustomerIP))  
                   {  
                       return CustomerIP;  
                   } 
      
                   CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
      
      
                   if (!String.IsNullOrEmpty(CustomerIP))  
                       return CustomerIP;  
      
                   if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)  
                   {  
                       CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
                       if (CustomerIP == null)  
                           CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
                  }  
                   else  
                   {  
                       CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
      
                   }    
                   if (string.Compare(CustomerIP, "unknown", true) == 0)  
                      return System.Web.HttpContext.Current.Request.UserHostAddress;  
                   return CustomerIP;  
               }  
               catch { }  
      
               return userIP;
        }
    
    </script>
     
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <%Response.Write(GetIPAddress()); %>
        </div>
        </form>
    </body>
    </html>
  • 相关阅读:
    Mishka and Interesting sum
    Tree Restoring
    LIB 配置文件读写器
    WCF 采用net.tcp协议实践
    MVC ViewEngineResult实际上是一种设计
    MVC 依赖注入扩展
    功能权限设计
    代理上网荟萃
    MD5算法 简介
    MVC _ViewStart文件的作用
  • 原文地址:https://www.cnblogs.com/net064/p/10244150.html
Copyright © 2020-2023  润新知