Active Directory Authentication

G

Guest

I have a login name with user name and password. based on provided username
and password, those infor will be looked up in the active directory for a
specific group to authenticate users. Could you please help me with that? i
have found a code to do that but fail to connect to the AD with provided
username and password. In order to connect to the AD, what access level is
needed here. Your help will be appreciate. Thanks,
 
W

Willy Denoyette [MVP]

|I have a login name with user name and password. based on provided
username
| and password, those infor will be looked up in the active directory for a
| specific group to authenticate users. Could you please help me with that?
i
| have found a code to do that but fail to connect to the AD with provided
| username and password. In order to connect to the AD, what access level
is
| needed here. Your help will be appreciate. Thanks,

There is no access level needed, you have to specify correct credentials and
UthenticationTypes when binding.
Please post your code.

Willy.
 
G

Guest

Here is the path. Is it corrrect?
"LDAP://companyname.org/OU=Office,DC=PDC,DC=companyname,DC=org"

Thanks,

public bool IsAuthenticatedUser(string domain, string userName, string
password)
{

//string domainAndUsername = domain + @"\" + userName;
string domainAndUsername = userName + "@" + domain;
// Connect to SHP AD from DirectoryEntry object
DirectoryEntry entry = new DirectoryEntry(_path,
domainAndUsername, password);

try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
// Get user from directory based on their login name
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + userName + ")";
search.PropertiesToLoad.Add("cn");
// If more than one entry is found, only return the first
entry. If no entry
// is found, return null
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("Error authenticating user. " +
ex.Message);
}
return true;
}

// Retrieve the list of group that a user is a member of the AD
public string GetUserGroup()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;

groupNames.Append("|");
for (int propertyCounter = 0; propertyCounter < propertyCount;
propertyCounter++)
{
dn =
(String)result.Properties["memberOf"][propertyCounter];

equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " +
ex.Message);
}
return groupNames.ToString();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top