在C#中,有没有一种方法可以检查应用程序是否正在本地主机上运行(而不是生产服务器上)?
我正在编写一个大规模邮件群发程序,如果它在本地主机上运行,则需要使用某个邮件队列。
if (Localhost)
{
Queue = QueueLocal;
}
else
{
Queue = QueueProduction;
}
在C#中,有没有一种方法可以检查应用程序是否正在本地主机上运行(而不是生产服务器上)?
我正在编写一个大规模邮件群发程序,如果它在本地主机上运行,则需要使用某个邮件队列。
if (Localhost)
{
Queue = QueueLocal;
}
else
{
Queue = QueueProduction;
}
那么可以考虑这样做:
public static bool OnTestingServer()
{
string host = HttpContext.Current.Request.Url.Host.ToLower();
return (host == "localhost");
}
在应用程序配置文件中使用一个值,可以告诉您当前所处的环境。
由于您正在使用asp.net,您可以利用配置文件转换来确保每个环境下设置正确。
web.config
是关于可变性的。我已经链接到了配置转换文档。我建议你阅读一下,这样你就能看到你可以做多少事情了。 - Oded看看这个是否有效:
public static bool IsLocalIpAddress(string host)
{
try
{ // get host IP addresses
IPAddress[] hostIPs = Dns.GetHostAddresses(host);
// get local IP addresses
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
// test if any host IP equals to any local IP or to localhost
foreach (IPAddress hostIP in hostIPs)
{
// is localhost
if (IPAddress.IsLoopback(hostIP)) return true;
// is local address
foreach (IPAddress localIP in localIPs)
{
if (hostIP.Equals(localIP)) return true;
}
}
}
catch { }
return false;
}
很遗憾,core 中不再有 HttpContext.HttpRequest.IsLocal()
这个方法了。
但是,在检查 .Net 的 原始实现 后,通过检查 HttpContext.Connection
,很容易重新实现相同的行为:
private bool IsLocal(ConnectionInfo connection)
{
var remoteAddress = connection.RemoteIpAddress.ToString();
// if unknown, assume not local
if (String.IsNullOrEmpty(remoteAddress))
return false;
// check if localhost
if (remoteAddress == "127.0.0.1" || remoteAddress == "::1")
return true;
// compare with local address
if (remoteAddress == connection.LocalIpAddress.ToString())
return true;
return false;
}
本地主机IP地址是恒定的,您可以使用它来判断是本地用户还是远程用户。
但是要注意,如果您已在生产服务器上登录,则也将被视为本地主机。
这适用于IP v.4和v.6:
public static bool isLocalhost( )
{
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
return (ip == "127.0.0.1" || ip == "::1");
}
public string GetMACAddress()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String sMacAddress = string.Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String.Empty)// only return MAC Address from first card
{
IPInterfaceProperties properties = adapter.GetIPProperties();
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
} return sMacAddress;
}
例如,与web.config中的MAC地址进行比较。
public static bool isLocalhost( )
{
return GetMACAddress() == System.Configuration.ConfigurationManager.AppSettings["LocalhostMAC"].ToString();
}
或者,如果你只是针对开发环境(假设你的应用程序不在生产中运行调试!),你可以使用C#预处理指令:
#if debug
Queue = QueueLocal;
#else
Queue = QueueProduction;
我知道这是一个非常老的帖子,但如果有人正在寻找一个直接的解决方案,那么你可以使用以下方法:
if (HttpContext.Current.Request.Url.Host == "localhost")
{
//your action when app is running on localhost
}
just like this:
HttpContext.Current.Request.IsLocal
这是一种更为透明的替代方案:
public static bool IsLocal
{
// MVC < 6
get
{
var authority = HttpContext.Request.Url.Authority.ToLower();
return authority == "localhost" ||
authority.StartsWith("localhost:");
}
// MVC 6+
get
{
return String.Compare(HttpContext.Request.Url.Host, "localhost",
StringComparison.OrdinalIgnoreCase);
}
}
如果您没有在Controller
中执行此操作,则需要在HttpContext
之后添加Current
,例如:HttpContext.Current.Request...
另外,在MVC 6
中,在View
中,HttpContext
只需使用Context
即可。