Environment.UserDomainName reports machine name instead of domain name

How can I get a hold of the local machine  I need the domain name and machine name.

Answer this question

Environment.UserDomainName reports machine name instead of domain name

  • Alan Homer

    There may be better ways, but here's code you can use to get the Domain\UserName:
    Imports System.Security.Principal

    Dim x As WindowsIdentity = WindowsIdentity.GetCurrent()
    Dim FullName As String = x.Name
    Then, you can parse the value in FullName, which will be in DOMAIN\USERNAME format.


  • Clabab

    I have a winform app in which I am trying to utilize the currently logged on users logon credentials. The username reports correctly but the function Environment.UserDomainName returns the machine name instead of the domain name, even though the user is in fact logged into the domin. I am running windows XP. shouldn't this function return the domain name  if not, the how to i retrieve this information
  • kada

    thanks. i wanted the DOMAIN\USERNAME format anyways.
  • Chris Mayo MS

    You would think that something named UserDomainName would return the domain name, but the documentation is pretty clear that that's not what it does. For example, the remarks for Environment.UserDomainName property include:

    "The value of this property is typically the host computer name, but can depend upon the application solution being deployed. The current user is the name specified in UserName."

    So, although the property is doing what the docs say it does, it doesn't do what you want. I know that you can get the domain name using P/Invoke using an ugly Win32 API call, but I'm not aware of the best way to do it using the .NET Framework.

    Working on researching retrieving the domain name, and will repost if I find it. Hopefully, someone else will post an answer before I get back to you!

  • db_cooper1950

    look in the <a href="http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemenvironmentmemberstopic.asp">System.Environment</a> Class
  • mohamamd

    If it's any help, I'm getting the same behavior: I'm logged into MYDOMAIN\UserName, but UserDomainName is returning MYMACHINE\UserName.

    The System.Security.Principal call works correctly. Looks like a bug in the framework.

    g.


  • Congero

    I beg to differ.  I believe the documentation is correct and so is the functionality.

    System.Environment.UserDomainName definitely does in fact return the domain that you are logged into.

    I just tried it on my WinXP machine (which is logged into my WinServer2K3 DC) and it definitely did return the domain name.

    When your computer is not part of a domain, the NETBIOS name of your computer IS in fact the "domain" that you are logged into.

    Since crowdozer said that it was returning the name of the computer, that in fact (to me) would technically mean that your computer is not logged into the domain.  Even if on the login screen you typed in your domain user name and password and have the domain selected in the drop down, that DOES NOT mean you are actually logged into the DC.  Windows caches that information and as of Windows 2000 and new versions of NT, if when you try to log in, it can not find the DC or the computer is not properly registered with the DC (an example of this that i've actually run into would be where you reload your DC with the same domain name and then relog into it, you're actually not, until you manually rejoin the PC to the DC), it will then check the cache to see if the last time you logged in, that was the correct username and password for that domain and if so, will let you in anyway.  But again, this doesn't technically mean you are logged into the DC.

    So, my guess is that's the problem.  Maybe try rejoining your computer to the DC and try again and see if that makes a difference.

  • SageLT

    Yep, totally agree. This ought to work. Apparently for you, it does. It's giving me back a computer name, rather than my domain name. 

    In any case, since the original requester is happy with DOMAIN\USERNAME, at least we can "close this thread". We can figure out the intricacies of the UserDomainName property offline.

  • Environment.UserDomainName reports machine name instead of domain name