Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get server domain name

Tags:

c#

.net

asp.net

dns

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"

like image 682
Sly Avatar asked Oct 21 '09 14:10

Sly


1 Answers

A bit late.. But the missing and best answer I have found, after having exactly the same issue:

private static string getComputerDomain()
        {
            try
            {
                return Domain.GetComputerDomain().Name;
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                return "Local (No domain)";
            }
        }

As stated on the msdn site:

This return value is independent of the domain credentials under which the application is run. This method will retrieve the computer's domain regardless of the trusted account domain credentials it is run under.

Tested on my personal pc (no AD-domain) and at work (with AD-domain) under IIS (anonymous access).

like image 158
Remco Avatar answered Oct 04 '22 22:10

Remco