jasminie

jasminie

  • NA
  • 78
  • 0

C# web ldap code use in windows

Sep 2 2011 12:48 PM

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;
}

}
}


Answers (6)