微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何确定Windows标识是否对应于本地或域用户?

我有一个WindowsIdentity,它对应于一个经过身份validation的用户。 如何确定身份是否与机器上的本地用户,已添加到本机的域用户或未添加到本机的域相对应?

让我们只是说我有3个用户帐户:

DomainUser(域用户组的成员,不添加到任何本地组)

LocalUser(在本机上创build的本地用户

MappedDomainUser(已添加到计算机上的组的域用户

我怎样才能区分

DomainUser和LocalUsers

LocalUser和MappedDomainUser

DomainUser和MappedDomainUser

截至目前我依赖于用户名,并检查是否以机器名称开始。 然后我通过检查用户所属的组(如果它是所有域用户的一部分)来进一步区分。 不是我确定的最好的方式。

Windows服务启动,停止,debugging问题

为什么Django用pype打开?

有没有去确定在Windows进程中可用的内存剩余量?

如何隐藏Windows中的Cygwin Python控制台窗口?

睡眠function在C中的窗口。 是否存在精度更高的函数

因为我有WindowsIdentity.User属性用户sid,我可以用它吗?

我如何将我的应用程序带到窗口顶部

在另一个文件上使用一个文件 – Python – Windows

推送gem托pipeCredentialed饲料失败

有没有一种简单的方法来强制Windows使用固定的DPI值来计算文本范围,而不是当前的DPI设置?

poco c ++库静态链接/ MT错误

不确定映射的域管理员。 我只是检查用户登录到域的本地和域管理员。 不要像“内建管理员”这样的字符串访问他们根据操作系统语言版本不同。

我喜欢使用.net 4.5校长的方法。 如果你可以使用4.5,你可以做类似的事情

所以关于这个问题我怎么区分呢?

DomainUser和LocalUsers

LocalUser和MappedDomainUser

DomainUser和MappedDomainUser

示例代码

using System; using System.DirectoryServices.ActiveDirectory; using System.Security.Principal namespace xxxxx { public class UserEnvTools { public static bool IsDomainAdmin() { //returns TRUE for a machine that is on a workgroup So consider GetDomain methods based on scenario if (WindowsIdentity.GetCurrent().User.AccountDomainSid == null) return false; var domainAdmins = new SecurityIdentifier(WellKNownSidType.BuiltinAdministratoRSSid,WindowsIdentity.GetCurrent().User.AccountDomainSid); var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); return prin != null && (prin.IsInRole(domainAdmins)); } public static bool IsDomainUser() { //returns TRUE for a machine that is on a workgroup So consider GetDomain methods based on scenario if (WindowsIdentity.GetCurrent().User.AccountDomainSid == null) return false; var domainUsers = new SecurityIdentifier(WellKNownSidType.BuiltinUseRSSid,WindowsIdentity.GetCurrent().User.AccountDomainSid); var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); return prin != null && (prin.IsInRole(domainUsers)); } public static bool IsLocalAdmin() { var localAdmins = new SecurityIdentifier(WellKNownSidType.BuiltinAdministratoRSSid,null); var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); return prin != null && (prin.IsInRole(localAdmins)); } public static bool IsLocalUser() { var localUsers = new SecurityIdentifier(WellKNownSidType.BuiltinUseRSSid,null); var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); return prin != null && (prin.IsInRole(localUsers)); } // Current security context applies public static Domain GetCurrentUserDomain() { try { return System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain(); } // It may be better not to ctach such errors? catch (ActiveDirectoryOperationException) // no Controller/AD Forest can not be contacted {return null;} catch (ActiveDirectoryObjectNotFoundException) // The USers Domain is not kNown to the controller {return null;} } public static Domain GetCurrentMachineDomain() { try { return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain(); } // It may be better not to ctach such errors? catch (ActiveDirectoryOperationException) // no controller or machine is not on a domain { return null; } catch (ActiveDirectoryObjectNotFoundException) // controller found,but the machine is not kNown { return null; } }

假设WindowsIdentity.Name工作方式与Environment.UserDomainName类似,如果用户名以机器名开头,那么它不在域上,否则它在域上。 这可以让你写

public static bool IsDomain(WindowsIdentity identity) { string prefix = identity.Name.Split('\')[0]; if (prefix != Environment.MachineName) return true; else return false; }

UserDomainName属性首先尝试获取当前用户的Windows帐户名称的域名组件。 如果该尝试失败,则此属性将尝试获取与由UserName属性提供的用户名关联的域名。 如果由于主机未连接到域而导致该尝试失败,则返回主机名称

您也可以针对计算机名称和域名相同的边缘情况筛选可用域列表(例如存储在数据库中)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐