Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查本地主机_C#_Asp.net_.net - Fatal编程技术网

C# 如何检查本地主机

C# 如何检查本地主机,c#,asp.net,.net,C#,Asp.net,.net,在c#中是否有方法检查应用程序是否在本地主机上运行(与生产服务器相反) 我正在编写一个群发邮件程序,当它在本地主机上运行时,需要使用特定的邮件队列 if (Localhost) { Queue = QueueLocal; } else { Queue = QueueProduction; } 比如说: public static bool OnTestingServer() { string host = HttpContext.Current.Request.Url.

在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,因此可以利用来确保设置对于您的每个环境都是正确的。

查看此设置是否有效:

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地址是常量,您可以使用它来确定它是本地主机还是远程用户

但是请注意,如果您登录到生产服务器,它也将被视为本地主机

这包括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");
}
为了完全确定代码在哪个服务器上运行,您可以使用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"); }
}
公共静态bool IsLocal
{
//MVC<6
获取{return HttpContext.Request.Url.Authority.Contains(“localhost”);}
//MVC6
获取{return HttpContext.Request.Host.Contains(“localhost”);}
}
如果您不是在
控制器中执行此操作,则在
HttpContext
之后添加
Current
,如
HttpContext.Current.Request…

另外,在
MVC6
中,在
视图中,
HttpContext
只是
上下文
或者,如果您只是针对一个开发环境(假设您的应用程序没有在生产调试中运行!),您可以使用:

由于a有正确的解决方案,我将把它作为一个答案发布:

HttpContext.Current.Request.IsLocal 
就这样,

HttpContext.Current.Request.IsLocal

不幸的是,core中已经没有了

但是在.Net中检查后,通过检查很容易重新实现相同的行为:


字符串hostName=Request.Url.Host.ToString()

我知道这是一条非常古老的线索,但仍然有人在寻找直接的解决方案,然后你可以使用这个:

if (HttpContext.Current.Request.Url.Host == "localhost")
{
  //your action when app is running on localhost
}

web应用程序始终在localhost上运行:)为什么不使用某种基于配置的值来指定正确的队列?它将在分配给它的位置运行,如果您对后端一无所知,则无法找到应用程序运行的位置。但是,任何正在运行的应用程序都必须有自己的系统,称为其localhost。尽管从总体上看,Oded的解决方案可能比简单地检查local host更适合。Request.Url.host可以返回其他内容,比如127.0.0.1。如果你想让你的代码独立于IIS,只需执行HttpContext.Current.Request.IsLocalit就可以避免依赖于HttpContext了……很有趣,但我认为我不能在web.config中存储变量,对吗?现在,邮件队列的路径是mailer服务中的一个字符串。@user547794-
web.config
是关于可变性的。我链接到了配置转换文档。我建议您阅读这篇文章,这样您就可以了解您能做多少。
.Contains(“localhost”)
不是一个好的解决方案,因为主机名可能是mylocalhost。最好检查一下
==“localhost”
当主机实际上是
localhost:1234
时,您的方式仍然正确吗?您是对的
Request.Url.Authority
返回主机:port,但
Request.Url.host
只返回主机。所以
返回String.Compare(HttpContext.Request.Url.Host,“localhost”,StringComparison.OrdinalIgnoreCase)是正确的答案。下面介绍如何从asp.NETCore2应用程序访问HTTPContext:它工作正常。在功能版本中,可能会将扩展方法或帮助程序添加到.NET Core中,以从box处理此验证。LocalIpAddress由于某种原因在nginx反向代理后面的Linux上为null。。。。自2012年8月以来已作为答案存在,如果您需要,我可以将其删除:)#nooffence
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;
}
if (HttpContext.Current.Request.Url.Host == "localhost")
{
  //your action when app is running on localhost
}