System.Environment.UserDomainName 按照微软的文档,解释的是 :
获取与当前用户关联的网络域名。
后面有一个备注:
该属性的值通常为主机名,但可能取决于所部署的应用程序解决方案。当前用户是 UserName 中指定的名称。
一般情况下如果我加入domain 的话,返回当前的domain。如果没有加入的话,则返回 当前机器名。
一般这个也是正确的,有时候,你会发现即使你加入域了,返回的也是 机器名。
稍微研究了发现这个是有原因的,userdomainname 其实是调用了一个 LookupAccountName , 这个 API 的解释也不太明确。
关于 domainname,ReferencedDomainName [out] Pointer to a buffer that receives the name of the domain where the account name is found. For computers that are not joined to a domain, this buffer receives the computer name. If this parameter is NULL, the function returns the required buffer size.
按说的话不会出现 即使你加入域了,返回的也是 机器名 的情况,其实还是出现了。
后来发现规律了,如果域用户在本机上有相同的用户名的话,则返回本地的机器名。碰到这种情况,删除该本地用户一切就ok了。
获取与当前用户关联的网络域名。
后面有一个备注:
该属性的值通常为主机名,但可能取决于所部署的应用程序解决方案。当前用户是 UserName 中指定的名称。
一般情况下如果我加入domain 的话,返回当前的domain。如果没有加入的话,则返回 当前机器名。
一般这个也是正确的,有时候,你会发现即使你加入域了,返回的也是 机器名。
稍微研究了发现这个是有原因的,userdomainname 其实是调用了一个 LookupAccountName , 这个 API 的解释也不太明确。
internal static extern bool LookupAccountName(string machineName, string accountName, byte[] sid, ref int sidLen, StringBuilder domainName, ref int domainNameLen, out int peUse);
关于 domainname,
按说的话不会出现 即使你加入域了,返回的也是 机器名 的情况,其实还是出现了。
后来发现规律了,如果域用户在本机上有相同的用户名的话,则返回本地的机器名。碰到这种情况,删除该本地用户一切就ok了。