简体   繁体   English

获取IIS网站的域名

[英]Getting IIS Websites' Domain names

I've created a web application to list all websites on IIS with their names, ports and physical paths. 我创建了一个Web应用程序,以列出IIS上的所有网站及其名称,端口和物理路径。 it's also supposed to get the IIS websites domain names. 它也应该获取IIS网站域名。 the application is installed on IIS and all functions work great except for returning domain names. 该应用程序安装在IIS上,除返回域名外,所有功能均正常运行。

I wrote the below code and it returned "localhost:8183" for all websites. 我编写了以下代码,并为所有网站返回了“ localhost:8183”。 8183 is the application's port itself. 8183是应用程序的端口本身。

  Request.Url.Scheme + "://" + Request.Url.Authority +
    Request.ApplicationPath.TrimEnd('/') + "/";here

then I tried this one and it just returned "localhost" instead of domain names. 然后我尝试了这个,它只返回了“ localhost”而不是域名。

  HttpContext.Current.Request.Url.Host;

I'm so curious to know where I'm going wrong. 我很想知道我要去哪里错了。 please let me know if any further information is needed. 请让我知道是否需要任何进一步的信息。

A web server does not actually know what domains it can serve. Web服务器实际上并不知道它可以服务的域。 It knows what domain was in the request headers in the thread for that request, but you can set up any domain you like to point to any server's IP address and the server will accept it. 它知道该请求的线程中的请求标头中的域是什么,但是您可以设置要指向任何服务器IP地址的任何域,服务器将接受它。

If you have set up bindings by domain (using Server Name Indication), you can get that information from the objects in Microsoft.Web.Administration ( for each site in ServerManager.Sites, for each Binding in site.bindings, if Binding.Protocol = http or https and Binding.Host !="" then write out Binding.Host) but that won't tell you about any domains that aren't in bindings. 如果已按域设置绑定(使用服务器名称指示),则可以从Microsoft.Web.Administration(对于ServerManager.Sites中的每个站点,对于site.bindings中的每个Binding,如果Binding.Protocol)中的对象获取信息。 = http或https以及Binding.Host!=“”,然后写出Binding.Host),但这不会告诉您任何不在绑定中的域。

If you use request object it will only give details about the website where your code is running. 如果使用请求对象,它将仅提供有关运行代码的网站的详细信息。 You need to use Directory Services to connect to IIS and do you work. 您需要使用目录服务连接到IIS,并且可以工作。

[From already answered question]- Here's an article explaining how this could be done using classes from the System.DirectoryServices namespace as well as the Microsoft.Web.Administration namespace which was introduced with IIS 7. [来自已经回答的问题]-这是一篇文章,说明如何使用System.DirectoryServices命名空间以及IIS 7引入的Microsoft.Web.Administration命名空间中的类来完成此操作。

You can use below code to get the Domain name of current server: 您可以使用以下代码获取当前服务器的域名:

System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName

or you can use below code to get current user's domain: 或者您可以使用以下代码获取当前用户的域:

System.Security.Principal.WindowsIdentity.GetCurrent().User. (string value)

Then you can trim it. 然后可以修剪它。

Happy coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM