我希望能够检查给定的用户名,如果该用户是域用户或机器的本地用户(最好使用.NET ),但可以在网上找到更多信息
public static Boolean isLocalUser (string name)
{
//code here
}编辑例如,您将获得一个字符串形式的me.user
发布于 2012-10-03 22:18:02
public bool DoesUserExist(string userName)
{
bool exists = false;
try
{
using (var domainContext = new PrincipalContext(ContextType.Domain, "DOMAIN"))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
{
exists = true;
}
}
}
catch(exception ex)
{
//Exception could occur if machine is not on a domain
}
using (var domainContext = new PrincipalContext(ContextType.Machine))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
{
exists = true;
}
}
return exists;
}发布于 2014-03-26 23:12:30
对我来说,到目前为止,这一切都运行得很好。(左侧使用离开)。
bool IsLocalUser(string accountName)
{
var domainContext = new PrincipalContext(ContextType.Machine);
return Principal.FindByIdentity(domainContext, accountName) != null;
}但请注意,这将无法识别“系统”帐户。我们单独检查它,因为它始终是本地帐户。
发布于 2012-10-03 22:18:06
David在赚钱,要检查当前用户是否是域的一部分,您可以检查Environment.UserDomainName并将其与当前用户进行比较。
有关更多信息,请访问MSDN
https://stackoverflow.com/questions/12710355
复制相似问题