C# 如何检查本地主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11834091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if localhost
提问by user547794
Is there a way in c# to check if the app is running on localhost (as opposed to a production server)?
c# 中有没有办法检查应用程序是否在本地主机上运行(而不是生产服务器)?
I am writing a mass mailing program that needs to use a certain mail queue is it's running on localhost.
我正在编写一个需要使用某个邮件队列的群发邮件程序,它在本地主机上运行。
if (Localhost)
{
Queue = QueueLocal;
}
else
{
Queue = QueueProduction;
}
采纳答案by Oded
Use a value in the application configuration file that will tell you what environment you are on.
在应用程序配置文件中使用一个值来告诉您您所在的环境。
Since you are using asp.net, you can utilize config file transformsto ensure the setting is correct for each of your environments.
由于您使用的是 asp.net,您可以利用配置文件转换来确保每个环境的设置都是正确的。
回答by ToddBFisher
What about something like:
怎么样:
public static bool OnTestingServer()
{
string host = HttpContext.Current.Request.Url.Host.ToLower();
return (host == "localhost");
}
回答by themanatuf
See if this works:
看看这是否有效:
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;
}
Reference: http://www.csharp-examples.net/local-ip/
回答by repeatdomiau
Localhost ip address is constant, you can use it to determines if it′s localhost or remote user.
localhost ip 地址是固定的,你可以用它来确定它是本地主机还是远程用户。
But beware, if you are logged in the production server, it will be considered localhost too.
但请注意,如果您登录到生产服务器,它也会被视为 localhost。
This covers IP v.4 and v.6:
这包括 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");
}
To be totally sure in which server the code is running at, you can use the MAC address:
要完全确定代码在哪个服务器上运行,您可以使用 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;
}
来自:http: //www.c-sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/
And compare with a MAC address in web.config for example.
例如,与 web.config 中的 MAC 地址进行比较。
public static bool isLocalhost( )
{
return GetMACAddress() == System.Configuration.ConfigurationManager.AppSettings["LocalhostMAC"].ToString();
}
回答by Serj Sagan
This works for me:
这对我有用:
public static bool IsLocal
{
// MVC < 6
get { return HttpContext.Request.Url.Authority.Contains("localhost"); }
// MVC 6
get { return HttpContext.Request.Host.Contains("localhost"); }
}
If you're notdoing this in the Controllerthen add Currentafter HttpContext, as in HttpContext.Current.Request...
如果你不这样做,在Controller再加入 Current后HttpContext,在HttpContext.Current.Request...
Also, in MVC 6, in the View, HttpContextis just Context
此外,在MVC 6,在View,HttpContext只是Context
回答by Phil Cooper
Or, you could use a C# Preprocessor Directiveif your simply targeting a development environment (this is assuming your app doesn't run in debug in production!):
或者,如果您只是针对开发环境(这是假设您的应用程序不在生产调试中运行!),您可以使用C# 预处理器指令:
#if debug
Queue = QueueLocal;
#else
Queue = QueueProduction;
回答by TombMedia
回答by Reinaldo
just like this:
像这样:
HttpContext.Current.Request.IsLocal
HttpContext.Current.Request.IsLocal
回答by Oliver
Unfortunately there is no HttpContext.HttpRequest.IsLocal()anymore within core.
不幸的是HttpContext.HttpRequest.IsLocal(),核心中不再有。
But after checking the original implementationin .Net, it is quite easy to reimplement the same behaviour by checking HttpContext.Connection:
但是在 .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;
}
回答by Oliver
string hostName = Request.Url.Host.ToString();
字符串主机名 = Request.Url.Host.ToString();

