我有一个在IIS 7服务器上运行的应用程序,在这个程序中我需要找到当前用户所属的所有组。当我使用服务器上的浏览器访问网站时,它运行正常,但是当我尝试从我的机器访问它时,它会不断抛出COM异常,这是我用来获取用户组的代码。
private List<string> GetUserGroups(string userName)
{
//The list of strings for output.
List<string> output= new List<string>();
try
{
//creating a PrincipalContext object in a using block for easy disposal
using(PrincipalContext domain = new PrincipalContext(ContextType.Domain,"domain"))
//using(WindowsIdentity user = WindowsIdentity.GetCurrent())
{
//Creating a UserPrincipal from the PrincipalContext by finding the user that
//was passed to the function
//This is the line that keeps throwing the exception.
using (UserPrincipal user = UserPrincipal.FindByIdentity(domain,IdentityType.SamAccountName,userName))
{
//Checking to make sure the user was found.
if (user != null)
{
//Getting the users groups in a collection variable called groups
PrincipalSearchResult<Principal> groups = UserPrincipal.Current.GetAuthorizationGroups();
//IdentityReferenceCollection groups = user.Groups;
//This foreach loop goes through each result in the groups collection
foreach (Principal p in groups)
{
//check the result is a GroupPrincipal object and is not null
if (p is GroupPrincipal && p.ToString() != null)
{
output.Add(p.ToString());//Add the string value to the output list.
debugString += "<br/>"+p.ToString();
}
}
}
}
}
}
catch (Exception ex)
{
processLog.Text += ex.ToString()+ ex.GetType();
}
//return the list of groups the user is a member of.
return output;
}
当我从服务器以外的位置访问异常时,为什么会抛出异常?我该如何解决?
更新: 这是stacktrace异常和所有
System.Runtime.InteropServices.COMException(0x80072020):An 发生操作错误。在 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(字符串 propertyName)at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() 在 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() 在 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() 在 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,IdentityType identityType,String identityValue)at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context,IdentityType identityType,String identityValue)at ResetUnlockAccount.ResetUnlockAccount.GetUserGroups(String userName) 在 C:\ ResetUnlockAccount \ ResetUnlockAccount \ ResetUnlockAccount.aspx.cs:行 894
答案 0 :(得分:0)
根据OP的评论,
答案在这里找到:GroupPrincipal method FindByIdentity throw strange exception
只需添加
using System.Web.Hosting;
和using(HostingEnvironment.Impersonate())
超过第一次使用 原始代码。