Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get domain name

My computer is in a Domain (Active Directory) and I need to get the domain name dynamically. I found the following code on the internet:

SelectQuery query = new SelectQuery("Win32_ComputerSystem"); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) {     foreach (ManagementObject mo in searcher.Get())     {         if ((bool)mo["partofdomain"])         {             this.Domain = mo["domain"].ToString();             break;         }     }  } 

It works exactly as I want and returns exactly the domain name as I want (when I am logged as Administrator). If the user is not a Domain Admin, I have an Access denied exception.

Does anybody know how to get the domain even with non-domain administrator users?

NOTE: I have found this solution on Internet System.Environment.UserDomainName; but it only gives me a part of the domain name.

I.e. my domain is: something.domain.com and the UserDomainName returns only something.

like image 769
Osvier Avatar asked Nov 12 '10 02:11

Osvier


People also ask

Can I get a domain for free?

You can get a free domain through a variety of ways, either by getting a web hosting plan, a site builder, an email hosting plan, or through a free domain registrar.

Can I get a domain from Google?

For countries where Google Domains is available, you can use Google Domains to purchase a domain. By default, Google Domains provides a one-year registration period and opts you in to auto-renew (step 6 below).

Is domain name available?

To check if a domain name is available, just type it in GoDaddy's search bar and we'll tell you immediately if someone else already owns it. If it's taken, you can search for your desired domain name using our WHOIS Lookup.


1 Answers

Why are you using WMI? Can't you use the standard .NET functionality?

System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName; 
like image 173
Pieter van Ginkel Avatar answered Sep 20 '22 14:09

Pieter van Ginkel