views:

328

answers:

4

Can anybody tell me how to get servers domain name in asp.net? (Environment.UserDomainName returns "IIS APPPOOL" string)

Thanks for replays, but mostly they are about DNS name of the server, what I need is the domain name. For example when I login via windows authentication I type domain\user and I need this "domain"

A: 

Try the System.Net.Dns class, it has plenty of helpful methods such as GetHostEntry i.e.:

var entry = System.Net.Dns.GetHostEntry("google.com"); // or vice-versa...
var name = System.Net.Dns.GetHostEntry("127.0.0.1"); // localhost ;)
John Leidegren
So then I guess the next question is how do you get your server's WAN IP? And what about the scenario where you're using shared hosting and therefore don't have an IP dedicated to your domain?
Tinister
I must have misunderstood "servers domain name", servers, in plural meant to me that somehow you want to perform domain name queries.
John Leidegren
+1  A: 

You'll need to extract it from the request object:

HttpContext.Current.Request.Url.Host
Tinister
+1: this is the cleanest way. It just returns the domain name, filtering out ports, http/https, path, querystring, etc.
Brian MacKay
Well, it returns the domain name used to access the server, that's probably fine, but it can be one of many different names used to access the server.
John Leidegren
A: 

There is specific problem to your question, there may be more than one domain name for a specific IP address.

As Tinister stated, you can use

HttpContext.Current.Request.Url.Host

But that will only tell you what the user wrote in the address bar of the browser. If the user added an entry to their host file for your site, and then use that host name, that is what you will see. (I have no idea why they would do so).

If you have more than one domain name for your web site, you can use that to figure out which of the domain names that the user requested.

Pete
Typical shared and reseller hosting companies use the same box for multiple customers, and therefore will configure their web servers to choose the correct site based on the host name. If someone changed their host file to try and fool the code in this fashion, it's likely that the web server will just reject the request upfront due to the unknown host name. Though I admit I've never actually tried doing this...
Tinister
That is true; I worked for a company where the same IIS server hosted several domain names in this fashion. I am merely pointing out that by inspecting the "Host" property you get what the user typed in, and there is no guarantees that this is indeed a correct value.
Pete
+2  A: 

It looks to me like you are trying to find the user's domain name. Since you are asking for the Environment.UserDomainName. Since your site is likely running with "Allow Anonymous Access" - the user isn't passing their domain information to the server and IIS is giving you the account information it does have, namely the app pool account.

Tom B