我System.DirectoryServices.AccountManagement
用来获取用户所属的所有组,下面是用于获取用户所有组的代码。
public void GetUserGroups(string userName)
{
UserPrincipal user = null;
using (HostingEnvironment.Impersonate())
{
// establish domain context
PrincipalContext domain = new PrincipalContext(ContextType.Domain);
// find your user
user = UserPrincipal.FindByIdentity(domain, userName);
}
// if found - grab its groups
if (user != null)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
}
}
这在我的开发服务器中效果很好,但是当代码部署在 IIS、DMZ 中的 UAT 服务器中时,此代码失败并显示以下堆栈跟踪:
Message: at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue)
at LandingPage.GetUserGroups(String userName)
at LandingPage.Page_Load(Object sender, EventArgs e)
我的问题与System.DirectoryServices.AccountManagement 在服务器上不起作用并使用相同的分辨率非常相似, 但没有任何帮助。
请指教。