内外网用户判断
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
/// <summary>
///Common 的摘要说明
/// </summary>
public class Common
{
public Common()
{
}
//导入判断网络是否连接的 .dll
[DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
/// <summary>
/// 判断网络状况的方法,返回值true为连接,false为未连接
/// </summary>
/// <returns></returns>
public extern static bool InternetGetConnectedState(out int conState, int reder);
/// <summary>
/// 取得客户端真实IP。如果有代理则取第一个非内网地址
/// by flower.b
/// </summary>
public static string IPAddress
{
get
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (result != null && result != String.Empty)
{
//可能有代理
if (result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式
result = null;
else
{
if (result.IndexOf(",") != -1)
{
//有“,”,估计多个代理。取第一个不是内网的IP。
result = result.Replace(" ", "").Replace("'", "");
string[] temparyip = result.Split(",;".ToCharArray());
for (int i = 0; i < temparyip.Length; i++)
{
if (IsIPAddress(temparyip[i])
&& temparyip[i].Substring(0, 3) != "10."
&& temparyip[i].Substring(0, 7) != "192.168"
&& temparyip[i].Substring(0, 7) != "172.16.")
{
return temparyip[i]; //找到不是内网的地址
}
}
}
else if (IsIPAddress(result)) //代理即是IP格式
return result;
else
result = null; //代理中的内容 非IP,取IP
}
}
string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (null == result || result == String.Empty)
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (result == null || result == String.Empty)
result = HttpContext.Current.Request.UserHostAddress;
return result;
}
}
/// <summary>
/// 返回用户类型()
/// </summary>
/// <returns></returns>
public static IPType UserIPType()
{
string userIp = IPAddress;
if (userIp == "127.0.0.1")//本机 (内网功能)
{
return IPType.Innner;
}
else if (userIp == "10.0.0.1")//内网(内网功能)
{
return IPType.Innner;
}
else//外网(外网功能)
{
return IPType.Out;
}
}
#region bool IsIPAddress(str1) 判断是否是IP格式
/**/
/// <summary>
/// 判断是否是IP地址格式 0.0.0.0
/// </summary>
/// <param name="str1">待判断的IP地址</param>
/// <returns>true or false</returns>
public static bool IsIPAddress(string str1)
{
if (str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15) return false;
string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";
Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
return regex.IsMatch(str1);
}
#endregion
/// <summary>
/// 用户IP类型
/// </summary>
public enum IPType
{
/// <summary>
/// 内网用户(含本机用户)
/// </summary>
Innner,
/// <summary>
/// 外网用户
/// </summary>
Out
}
}
调用
string url ="";
//内网用户
if (Common.UserIPType()==Common.IPType.Innner)
{
url= "http://172.16.10.120/xxx";
PageContext.RegisterStartupScript("window.open('" + url + "','_blank','');");
}
else //外网用户
{
url = "http://xxx.example.com.cn/xxx";
PageContext.RegisterStartupScript("window.open('" + url + "','_blank','');");
}
总有一些人过着你想要的生活