为什么 System.Environment.MachineName 值是大写的?

发布于 2024-10-08 13:58:05 字数 216 浏览 0 评论 0原文

我的计算机名称是小写的(我在“高级系统设置”对话框的“计算机名称”选项卡中看到),但 System.Environment.MachineName 报告它是大写的。这是为什么?这对我来说是一个真正的问题,因为从我的测试来看,PrincipalPermissionAttribute 对角色名称执行区分大小写的比较(我将自定义角色映射到 Windows 组,并且我的环境是非域)。有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-10-15 13:58:05

.NET 4.7.1 的 Environment.MachineName 源代码位于:https://referencesource.microsoft.com/#mscorlib/system/environment.cs,be0b5c103d248dce

它 p/invokes GetComputerName 如下所示:https://referencesource.microsoft.com/#mscorlib/microsoft/win32/win32native.cs,0c7d7f4f83d4ddd0

这是 GetComputerName 函数: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724295(v=vs.85).aspx,其中指出:

GetComputerName 仅检索本地计算机的 NetBIOS 名称
检索 DNS 主机名、DNS 域名或完全限定的
DNS名称,调用GetComputerNameEx函数。

计算机名称的 MSDN,https://msdn.microsoft。 com/en-us/library/ms724220(VS.85).aspx,指出:

NetBIOS 名称最多由 15 个字节的 OEM 字符组成,包括
字母、数字、连字符和句点。有些字符特定于
字符集。 NetBIOS 名称通常在 OEM 中表示
字符集。 OEM 字符集取决于区域设置。一些OEM
字符集将某些字符表示为两个字节。网络BIOS
按照惯例,名称以大写形式表示,其中
小写到大写的转换算法是 OEM 字符集
依赖。

因此,按照惯例,NetBIOS 名称为大写,System.Environment.MachineName 返回系统的 NetBIOS 名称。

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.

满地尘埃落定 2024-10-15 13:58:05

使用 Dns.GetHostName 代替,应该返回它具有正确的大小写(至少在我的计算机上是这样)。

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

我不在是我 2024-10-15 13:58:05

根据这篇 MSDN 文章,它不区分大小写
http://msdn.microsoft.com/en-us/ library/ms724220(VS.85).aspx

在什么情况下,它进行区分大小写的比较?

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文