Click here to Skip to main content
15,609,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all,

I have a active directory search in my project and works fine when i run from visual studio. But when i host the same in IIS it does not work.

Can anybody please help?

The following is the code i use
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(string.Format("LDAP://{0}", Environment.UserDomainName));
System.DirectoryServices.DirectorySearcher dSearch = new System.DirectoryServices.DirectorySearcher(entry);
//dSearch.Filter = string.Format("(&(objectClass=User)(|(cn={0})(sAMAccountName={0})))", objUserInformation.EmpID);
dSearch.Filter = string.Format("(&(objectClass=User)(|(cn={0})(sAMAccountName={0})))", "E455321");



this.ClientSideAlertMessage("After col add");

foreach (object objProperty in dSearch.FindOne().Properties["directReports"])
    var objProperty1 = objProperty.ToString();
    var subordinatename = objProperty1.Split(',').Select(pair => pair.Split('=').LastOrDefault()).ToArray().GetValue(0);
    System.DirectoryServices.AccountManagement.PrincipalContext ctx = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain);
    System.DirectoryServices.AccountManagement.UserPrincipal user = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(ctx, System.DirectoryServices.AccountManagement.IdentityType.DistinguishedName, objProperty1);
    //Adding rows to the datatable
    DataRow dr;
    dr = dt.NewRow();
    dr[0] = user.DisplayName;
    DataRow drEmpDet;
    drEmpDet = dtEmpDet.NewRow();
    UserManager objUserManager = new UserManager();
    drEmpDet[0] = objUserManager.GetEmpEid(user.EmailAddress);
    drEmpDet[1] = user.EmailAddress;

Thanks in advance,
ZurdoDev 9-Oct-13 10:34am    
1. Post the error. Otherwise we just guess.
2. Since you have no error, my guess is permissions. If it runs from your machine and then doesn't from IIS it is likely permissions issue. The account that is running the app pool will need permissions.
thatraja 9-Oct-13 11:42am    
Possible solutions
phil.o 9-Oct-13 12:06pm    
Application pool's permissions on AD likely is the problem. My virtual 5! ^^

1 solution

Actually the domain name it took a different one when i run from Visual Studio and a different one when i run from IIS.

I identified it and got it cleared.

Thanks all for your comments
Share this answer
Sowjanya49183 9-Jan-14 7:20am    
Could you please provide the code snippet how you resolved the issue. Because now i have the same type of issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900