- using System;
- using System.Collections;
- using System.DirectoryServices;
-
- namespace FormsAuthAD
- {
- /// <summary>
- /// Description résumée de ActiveDirectory.
- /// </summary>
- public class LdapAuthentication
- {
- public LdapAuthentication()
- {
- //
- // TODO : ajoutez ici la logique du constructeur
- //
- }
-
- public LdapAuthentication( string path )
- {
- _path = path;
- }
-
- private string _path;
- private string _filterAttribute;
-
- public bool IsAuthenticated(string domain, string username, string pwd)
- {
- string domainAndUsername = domain + @"\" + username;
- DirectoryEntry entry = new DirectoryEntry( _path,
- domainAndUsername,
- pwd);
-
- 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("Error authenticating user. " + ex.Message);
- }
- return true;
- }
-
-
- }
- }
-
-
- // UTILISATION DE LA CLASS EXEMPLE :
- bool bResult = false;
- LdapAuthentication MyAD = new LdapAuthentication( "LDAP://AdresseOuNomduserveurdedomaine" );
-
- try
- {
- bResult = MyAD.IsAuthenticated( "Nomdudoaine","login", "Motdepasse" );
- }
- catch( Exception ExErreur )
- {
- messageLabel.Text = "Erreur Active Directory : "+ExErreur.Message;
- }
using System;
using System.Collections;
using System.DirectoryServices;
namespace FormsAuthAD
{
/// <summary>
/// Description résumée de ActiveDirectory.
/// </summary>
public class LdapAuthentication
{
public LdapAuthentication()
{
//
// TODO : ajoutez ici la logique du constructeur
//
}
public LdapAuthentication( string path )
{
_path = path;
}
private string _path;
private string _filterAttribute;
public bool IsAuthenticated(string domain, string username, string pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername,
pwd);
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("Error authenticating user. " + ex.Message);
}
return true;
}
}
}
// UTILISATION DE LA CLASS EXEMPLE :
bool bResult = false;
LdapAuthentication MyAD = new LdapAuthentication( "LDAP://AdresseOuNomduserveurdedomaine" );
try
{
bResult = MyAD.IsAuthenticated( "Nomdudoaine","login", "Motdepasse" );
}
catch( Exception ExErreur )
{
messageLabel.Text = "Erreur Active Directory : "+ExErreur.Message;
}