我有一个WindowsIdentity,它对应于一个经过身份validation的用户。 如何确定身份是否与机器上的本地用户,已添加到本机的域用户或未添加到本机的域相对应?
让我们只是说我有3个用户帐户:
我怎样才能区分
截至目前我依赖于用户名,并检查是否以机器名称开始。 然后我通过检查用户所属的组(如果它是所有域用户的一部分)来进一步区分。 不是我确定的最好的方式。
因为我有WindowsIdentity.User属性的用户sid,我可以用它吗?
不确定映射的域管理员。 我只是检查用户是登录到域的本地和域管理员。 不要像“内建\管理员”这样的字符串访问他们根据操作系统语言版本不同。
我喜欢使用.net 4.5校长的方法。 如果你可以使用4.5,你可以做类似的事情
所以关于这个问题我怎么区分呢?
示例代码
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属性提供的用户名关联的域名。 如果由于主机未连接到域而导致该尝试失败,则返回主机名称。
您也可以针对计算机名称和域名相同的边缘情况筛选可用域列表(例如存储在数据库中)。