Tuesday 25 October 2016

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



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 GetUserGroups(string userName)
{
//The list of strings for output.
List output= new List();
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 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 += "
"+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



Answer



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.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...