6

My machine name is lowercased (i see that in Advanced system settings dialog, Computer Name tab) but System.Environment.MachineName reports it uppercased. Why is that? This is a real problem for me because from my tests PrincipalPermissionAttribute performs case sensitive comparison for role names (i map my custom roles to Windows groups and my environment is non-domain). Any advise?

UserControl
  • 14,766
  • 20
  • 100
  • 187

3 Answers3

7

The source for Environment.MachineName for .NET 4.7.1 is here: https://referencesource.microsoft.com/#mscorlib/system/environment.cs,be0b5c103d248dce

It p/invokes GetComputerName as seen here: https://referencesource.microsoft.com/#mscorlib/microsoft/win32/win32native.cs,0c7d7f4f83d4ddd0

Here is the GetComputerName function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724295(v=vs.85).aspx, which states:

GetComputerName retrieves only the NetBIOS name of the local computer. To retrieve the DNS host name, DNS domain name, or the fully qualified DNS name, call the GetComputerNameEx function.

The MSDN for Computer Names, https://msdn.microsoft.com/en-us/library/ms724220(VS.85).aspx, states:

NetBIOS names consist of up to 15 bytes of OEM characters including letters, digits, hyphens, and periods. Some characters are specific to the character set. NetBIOS names are typically represented in the OEM character set. The OEM character set depends on the locale. Some OEM character sets represent certain characters as two bytes. NetBIOS names, by convention, are represented in uppercase where the translation algorithm from lowercase to uppercase is OEM character set dependent.

So, NetBIOS names are upper case by convention and System.Environment.MachineName returns the system's NetBIOS name.

Quantic
  • 1,779
  • 19
  • 30
5

Use Dns.GetHostName instead, that should return it with the correct case (at least it does on my computer).

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • Thanks, worked for me! Is it possible that Dns.GetHostName() may return a name different from NETBIOS name ('cause i use it to construct local Windows group names)? – UserControl Dec 18 '10 at 14:13
  • @User: Well, `GetHostName` will return the DNS hostname, but I can't remember how that relates to the netbios name. – Hans Olsson Dec 18 '10 at 14:20
  • 1
    If anyone is interested (as I was) [here's the native method](https://msdn.microsoft.com/en-us/library/windows/desktop/ms738527(v=vs.85).aspx) that is called to get the result. Has some details about the process that may be relevant if you plan on using this. –  Apr 06 '15 at 19:58
1

According to this MSDN article, its case - insensitive http://msdn.microsoft.com/en-us/library/ms724220(VS.85).aspx

In which scenario, it is doing case sensitive comparison ?

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • I've just retested it and can confirm that if i have role as "MYBOX\SupportPersonnel" PrincipalPermission throws the exception but in case of "mybox\SupportPersonnel" everything works as expected. My environment is Windows 7 x64. – UserControl Dec 18 '10 at 14:09