获取客户端IP, 生成 GUID
using System;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace QinLanRegionalHealth.Helper
{
public class PubConstant
{
/// <summary>
/// 获取连接字符串
/// </summary>
public static string ConnectionString
{
get
{
string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];
return _connectionString;
}
}
/// <summary>
/// 得到web.config里配置项的数据库连接字符串。
/// </summary>
/// <param name="configName"></param>
/// <returns></returns>
public static string GetConnectionString(string configName)
{
string connectionString = ConfigurationManager.AppSettings[configName];
return connectionString;
}
public enum LogMode
{
NoLog,
LogEasy,
LogSQL
}
/// <summary>
/// 配置文件中 日志记录模式【0代表不记录,1代表简单记录,2代表详细记录,包含sql语句等】
/// </summary>
public static string LogModeConfig
{
get
{
string _LogMode = ConfigurationManager.AppSettings["LogMode"];
//若不是012,则强制赋值为0,不进行日志记录
if (_LogMode != "0" && _LogMode != "1" && _LogMode != "2")
{
_LogMode = "0";
}
return _LogMode;
}
}
#region 获取web客户端ip
/// <summary>
/// 获取web客户端ip【在用】
/// </summary>
/// <returns></returns>
public static string GetWebClientIp()
{
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 simone 090805
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;
}
private static string localIpAdd;
/// <summary>
/// 获取客户端的IP地址
/// </summary>
public static string LocalIpAdd
{
get
{
#region MyRegion
////StringBuilder sbIPadds = new StringBuilder();
////string hostname = Dns.GetHostName();//得到本机名
//////IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已过期,只得到IPv4的地址 <SPAN style="WHITE-SPACE: pre"> </SPAN>
////IPHostEntry localhost = Dns.GetHostEntry(hostname);
//////IPAddress localaddr = localhost.AddressList[0];
////foreach (IPAddress ip in localhost.AddressList)
////{
//// if (ip.AddressFamily.ToString() == "InterNetwork")
//// {
//// sbIPadds.Append(ip.ToString());
//// }
////}
////localIpAdd = sbIPadds.ToString();
////return localIpAdd;
#endregion
localIpAdd = GetWebClientIp();
return localIpAdd;
}
}
//获取本机的公网IP
public static string GetWWWIP()
{
string tempip = "";
try
{
WebRequest wr = WebRequest.Create("http://city.ip138.com/ip2city.asp");
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int start = all.IndexOf("[") + 1;
int end = all.IndexOf("]", start);
tempip = all.Substring(start, end - start);
sr.Close();
s.Close();
}
catch
{
}
return tempip;
}
#endregion 获取web客户端ip
#region GUID生成
/// <summary>
/// GUID生成
/// </summary>
/// <returns></returns>
public static string MakeGUID()
{
return Guid.NewGuid().ToString();
}
/// <summary>
/// GUID批量生成
/// </summary>
/// <param name="count">个数</param>
/// <returns>格式为 string[]</returns>
public static string[] MakeGUID(int count)
{
string[] guidArray = new string[] { };
if (count != 0)
{
for (int i = 0; i < count; i++)
{
guidArray[i] = Guid.NewGuid().ToString();
}
}
return guidArray;
}
#endregion GUID生成
}
}