i am trying to make the following web code work for a C#.net 2008 desktop application. My company wants us to use the same logic to check groups in the active directory. The following is the code snippet I obtained from the contract.
From the contractor, i also obtained the xml file. The three columns i am referring to are not in the xml file. The fields I am trying to figure out how they are referenced in the app code are the following:
DirectoryEntry
DirectorySearcher
SearchResult.
Would you have any idea what I can use to point to the columns listed above are that I can use in a C#.net 2008 desktop application? if so, can you show me some code and/or point to a url that I can use as a reference?
The following is the code i am trying to work with:
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace Sup
{
public class ActiveDirectoryValidator
{
private string _path;
private string _filterAttribute;
public ActiveDirectoryValidator(string path)
{
_path = path;
}
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;
}
}
}

VulpesRank #55 · 29.2K reputationPosted Sep 2, 2011, 1:58 PM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 2, 2011, 10:24 AM EDT
are classes in the System.DirectoryServices namespace (see http://msdn.microsoft.com/en-us/library/system.directoryservices.aspx).
As well as a using directive for this namespace (to avoid having to use fully-qualified names), you'll also need to add a reference to System.DirectoryServices.dll to your project in order use these classes.
Otherwise the code should work "as is".
jasminie78 reputationPosted Sep 2, 2011, 10:47 AM EDT
My problem is in the
public ActiveDirectoryValidator(string path)
{
_path = path;
}
What kind of a path needs to be passed here so the code works? i am guessing this is the ldap directory?Should the windows application be able to find this path write away? If so, how would I incorporate the code in a desktop class file? What would I need to do specifically to call this class?
If this code does not work, what do i need to pass to this code? The directory path of the ldap code?
VulpesRank #55 · 29.2K reputationPosted Sep 2, 2011, 11:34 AM EDT
To make use of this class, I'd imagine you'd do this:
ActiveDirectoryValidator validator = new ActiveDirectoryValidator(LDAPPath);
bool isAuthenticated = validator.IsAuthenticated(domain, user, password);
Notice also that this class is in the Sup namespace so, if you're going to use it "as is", you'll need the using directive:
using Sup;
in other files in your project.
jasminie78 reputationPosted Sep 2, 2011, 1:20 PM EDT
jasminie78 reputationPosted Sep 2, 2011, 8:18 PM EDT