简体   繁体   中英

Getting IIS Websites' Domain names

I've created a web application to list all websites on IIS with their names, ports and physical paths. it's also supposed to get the IIS websites domain names. the application is installed on IIS and all functions work great except for returning domain names.

I wrote the below code and it returned "localhost:8183" for all websites. 8183 is the application's port itself.

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

then I tried this one and it just returned "localhost" instead of domain names.

  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. 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.

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.

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.

[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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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