【发布时间】:2012-03-13 11:46:47
【问题描述】:
我可以通过下一行代码检查用户域管理员:
using (Impersonation im = new Impersonation(UserName, Domain, Password))
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
bool isDomainAdmin = identity.IsDomainAdmin(Domain, UserName, Password);
if (!isDomainAdmin)
{
//deny access, for example
}
}
其中 IsDomainAdmin - 是扩展方法
public static bool IsDomainAdmin(this WindowsIdentity identity, string domain, string userName, string password)
{
Domain d = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, domain, userName, password));
using (DirectoryEntry de = d.GetDirectoryEntry())
{
byte[] domainSIdArray = (byte[])de.Properties["objectSid"].Value;
SecurityIdentifier domainSId = new SecurityIdentifier(domainSIdArray, 0);
SecurityIdentifier domainAdminsSId = new SecurityIdentifier(WellKnownSidType.AccountDomainAdminsSid, domainSId);
WindowsPrincipal wp = new WindowsPrincipal(identity);
return wp.IsInRole(domainAdminsSId);
}
}
但是,当 IsDomainAdmin 方法被调用时,它试图将一些文件写入模拟用户的 %LOCALAPPDATA%,如果程序不是以管理员身份运行,则会引发异常
无法加载文件或程序集'System.DirectoryServices, 版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 或 它的依赖项之一。没有达到要求的模拟级别 提供,或提供的模拟级别无效。 (例外 来自 HRESULT:0x80070542)
【问题讨论】:
标签: c# .net active-directory