Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exception while accessing AD in Service Layer
- private SearchResult GetUserById(string id)
- {
- SearchResult result = null;
- string targetEnviroment = this.serviceApplication.TargetEnvironment;
- try
- {
- DirectorySearcher ldapSearch = new DirectorySearcher();
- ldapSearch.SearchRoot = this.GetDirectoryEntry();
- if (targetEnviroment == "XYZ")
- {
- ldapSearch.Filter = "(&(objectClass=person)(userProperty=" + id + "))";
- }
- else
- {
- ldapSearch.Filter = "(&(objectClass=user)(samAccountName=" + id + "))";
- }
- ldapSearch.SearchScope = SearchScope.Subtree;
- result = ldapSearch.FindOne();
- }
- catch (Exception ex)
- {
- throw;
- }
- return result;
- }
- public CorpDirUser CorpDir_GetUserById(string userId)
- {
- CorpDirUser result = null;
- string targetEnviroment = this.serviceApplication.TargetEnvironment;
- try
- {
- DirectoryEntry entry = this.GetUserById(userId).GetDirectoryEntry();
- if (entry != null)
- {
- if (targetEnviroment == "XYZ")
- {
- result = MapToUserFromXYZLDAP(entry);
- }
- else
- {
- result = MapToUserFromZYXLDAP(entry);
- }
- }
- }
- catch (Exception ex)
- {
- // logging
- }
- return result;
- }
- private DirectoryEntry GetDirectoryEntry()
- {
- string pathath = this.serviceApplication.CorpDirLDAPPath;
- string account = this.serviceApplication.CorpDirServiceAccountName;
- string password = this.serviceApplication.CorpDirServiceAccountPassword;
- return new DirectoryEntry("LDAP://" + path, account, password, AuthenticationTypes.None);
- }
Add Comment
Please, Sign In to add comment