在c#中有没有办法检查应用程序是否在本地主机(而不是生产服务器)上运行?如何检查本地主机
我正在写一个群发邮件程序,需要使用某个邮件队列,它在本地主机上运行。
if (Localhost)
{
Queue = QueueLocal;
}
else
{
Queue = QueueProduction;
}
在c#中有没有办法检查应用程序是否在本地主机(而不是生产服务器)上运行?如何检查本地主机
我正在写一个群发邮件程序,需要使用某个邮件队列,它在本地主机上运行。
if (Localhost)
{
Queue = QueueLocal;
}
else
{
Queue = QueueProduction;
}
应用程序配置文件,该文件会告诉你,你是在什么环境下使用的值。
由于您使用的是asp.net,因此您可以使用config file transforms来确保您的每个环境的设置都是正确的。
有趣,但我没想到我可以存储变量内的web.config,我可以?现在,邮件队列的路径是邮件服务中的一个字符串。 – user547794 2012-08-06 19:05:50
@ user547794 - 'web.config'全部关于可变性。我链接到配置转换文档。我建议你阅读一下,这样你就可以看到你能做多少事情。 – Oded 2012-08-06 19:07:10
什么是这样的:
public static bool OnTestingServer()
{
string host = HttpContext.Current.Request.Url.Host.ToLower();
return (host == "localhost");
}
尽管从总体上看,Oded的解决方案很可能比单纯检查本地主机更倾向于采用。 – ToddBFisher 2012-08-06 18:57:17
Request.Url.Host可以返回其他内容,如127.0.0.1。只要做HttpContext.Current.Request.IsLocal – mhenry1384 2012-12-11 22:36:39
最好避免依赖于HttpContext,如果你想你的代码独立于IIS ... – abzarak 2016-02-13 18:11:20
见,如果这个工程:
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;
}
本地主机IP地址是固定的,你可以用它来确定本地主机it's或远程用户。
但请注意,如果您登录了生产服务器,它也将被视为本地主机。
这包括IP第4节和第6节:
public static bool isLocalhost()
{
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
return (ip == "127.0.0.1" || ip == "::1");
}
要完全确定在哪个服务器的代码在运行时,您可以使用MAC地址:从
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();
}
这个工作对我来说:
public static bool IsLocal
{
// MVC < 6
get { return HttpContext.Request.Url.Authority.Contains("localhost"); }
// MVC 6
get { return HttpContext.Request.Host.Contains("localhost"); }
}
如果你不在在Controller
这样做,然后HttpContext
之后添加Current
,如HttpContext.Current.Request...
此外,在MVC 6
,在View
,HttpContext
只是Context
'.Contains(“localhost”)'不是一个好的解决方案,因为主机名可以是_mylocalhost_。更好地检查'==“localhost”' – 2016-05-27 10:19:33
什么时候主机实际上是'localhost:1234'你的方式仍然是真的吗? – 2016-05-27 14:44:11
你是对的'Request.Url.Authority' [MSDN](https://msdn.microsoft.com/en-us/library/system.uri.Authority(v = vs.110).aspx)返回主机:端口,但'Request.Url.Host' [MSDN](https://msdn.microsoft.com/en-us/library/system.uri.host(v = vs.110).aspx) - 只是主机。 所以 '返回String.Compare(HttpContext.Request.Url.Host,“本地主机”,StringComparison.OrdinalIgnoreCase);'是正确的答案,如果你不关心回送IP的 – 2016-06-03 14:24:00
或者,你可以使用一个C# Preprocessor Directive如果你的目标只是一个开发环境(这是假设你的应用程序不调试在生产中运行!):
#if debug
Queue = QueueLocal;
#else
Queue = QueueProduction;
作为comment有正确的解决方案,我要发布它作为一个答案:
HttpContext.Current.Request.IsLocal
就像这样:
HttpContext.Current.Request。IsLocal
不幸的是,核心内不再有HttpContext.HttpRequest.IsLocal()
。
但经过检查.NET中的original implementation,这是很容易通过检查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;
}
网络应用程序总是在本地主机上运行:) – 2012-08-06 18:53:18
为什么不使用某些类型的配置基于指定正确队列的值? – 2012-08-06 18:53:22
如果您不知道关于后端的任何信息,那么您将无法找到应用程序正在运行的位置,它将运行在已分配给它的位置。但是,任何正在运行的应用程序都必须拥有自己的系统,并将其称为本地主机。 – perilbrain 2012-08-06 18:56:40