dc

dc

  • NA
  • 663
  • 0

Active dirtectory search

Sep 6 2011 11:29 AM
I have a C#.net windows form application 2008 application. I would like to know how to make the following code work so I can see if users belong to three different groups in the active directory. i was told by my company that I need to make this code work.
The code is called from:
CurUser = new ActiveDirectoryUser();
bool isAuthenticated = CurUser.IsAuthenticated(domain, user, password);
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace Common.Area
{
public class ActiveDirectoryUser
{
private string _path;
private string _filterAttribute;
//public ActiveDirectoryUser(string path)
public ActiveDirectoryUser()
{
//_path = path;
_path = "LDAP://Omaha.Reed/DC=OMAHA,DC=REED";
}
public bool IsAuthenticated(string domainName, string userName, string password)
{
string domainAndUsername = domainName + @"\" + userName;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, password);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + userName + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

if (null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return true;
}
}
}

Answers (3)