2

我有一个在 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;
}

为什么当我从服务器以外的位置访问它时会引发异常?我该如何解决?

更新:这是堆栈跟踪异常和所有

System.Runtime.InteropServices.COMException (0x80072020):发生操作错误。在 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 在 System.DirectoryServices.DirectoryEntry.Bind() 在 System.DirectoryServices.DirectoryEntry.get_AdsObject() 在 System.DirectoryServices.PropertyValueCollection.PopulateList() 在 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 System.DirectoryServices.AccountManagement.PrincipalContext 中的 .PrincipalContext.Initialize()。

4

1 回答 1

0

根据OP的评论,

答案在这里找到:GroupPrincipal 方法 FindByIdentity 抛出奇怪的异常

只需在原始代码中添加using System.Web.Hosting;并 覆盖第一次使用。using(HostingEnvironment.Impersonate())

于 2014-06-16T20:21:05.437 回答