Shadi M.
Shadi M.

Reputation: 13

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.

Upvotes: 0

Views: 2513

Answers (3)

Dave Tinney
Dave Tinney

Reputation: 11

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.

Upvotes: 1

badiyev
badiyev

Reputation: 1

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.

Upvotes: 0

Gaurav Sharma
Gaurav Sharma

Reputation: 586

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.

Upvotes: 0

Related Questions