【问题标题】:asp.net application userprincipal.findbyidentity works with browser on server, throws exception from my machineasp.net 应用程序 userprincipal.findbyidentity 与服务器上的浏览器一起使用,从我的机器抛出异常
【发布时间】:2014-08-06 15:01:36
【问题描述】:

我有一个在 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(布尔 throwIfFail)在 System.DirectoryServices.DirectoryEntry.Bind() 在 System.DirectoryServices.DirectoryEntry.get_AdsObject() 在 System.DirectoryServices.PropertyValueCollection.PopulateList() 在 System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry 条目,字符串属性名称)在 System.DirectoryServices.PropertyCollection.get_Item(字符串 属性名称)在 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 上下文,类型 principalType,Nullable`1 身份类型,字符串 身份值,日期时间参考日期)在 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext 上下文,类型 principalType,IdentityType identityType,字符串 身份值)在 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext 上下文,身份类型身份类型,字符串身份值)在 ResetUnlockAccount.ResetUnlockAccount.GetUserGroups(字符串用户名) 在 C:\ResetUnlockAccount\ResetUnlockAccount\ResetUnlockAccount.aspx.cs:line 894

【问题讨论】:

  • 是否存在 COM 异常正在包装的 InnerException 对象?
  • @LynnCrumbling,我试图打印出来,它说我试图访问一个空值,所以不,我不这么认为,我会再试一次以确保。跨度>
  • 在你的catch中,你可以为(ex.InnerException != null)添加一个检查;如果是这样的话,processLog.Text += ex.InnerException.ToString());
  • 我就是这么做的,没有内部异常,out异常是System.Runtime.InteropServices.COMException
  • COM 应该给你一个错误代码。无论是十进制还是十六进制。这对于谷歌搜索将是无价的。

标签: asp.net iis-7 directoryservices active-directory-group userprincipal


【解决方案1】:

根据 OP 的评论,

答案在这里找到了:GroupPrincipal method FindByIdentity throw strange exception

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多