asp.net application userprincipal.findbyidentity works with browser on server, throws exception from my machine

前提是你 提交于 2020-01-04 15:13:53

问题


I have an application that is running on an IIS 7 server, in this program I need to find all the groups that the current user is a member of. When I access the website using the browser on the server, it works perfectly, but when I try to access it from my machine it keeps throwing a COM exception, Here is the code I'm using to get the user groups.

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;
}

Why does it throw the exception when I access it from a location other than the server? How can I fix it?

Update: Here is the stacktrace exception and all

System.Runtime.InteropServices.COMException (0x80072020): An operations error occurred. 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, IdentityType identityType, String identityValue) at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) at ResetUnlockAccount.ResetUnlockAccount.GetUserGroups(String userName) in C:\ResetUnlockAccount\ResetUnlockAccount\ResetUnlockAccount.aspx.cs:line 894


回答1:


Per the OP's comment,

The answer was found here: GroupPrincipal method FindByIdentity throw strange exception

Just had to add using System.Web.Hosting; and using(HostingEnvironment.Impersonate()) over the first using in the original code.



来源:https://stackoverflow.com/questions/24249586/asp-net-application-userprincipal-findbyidentity-works-with-browser-on-server-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!