IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

[C#]Authentification active directory d'un user


Sujet :

ASP.NET

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    801
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 801
    Points : 314
    Points
    314
    Par défaut [C#]Authentification active directory d'un user
    Bonjour à tous,
    Voilà, je n'arrive pas à me dépatouiller de ce problème.
    Je voudrais vérifier qu'un user a bien rentré son mot de passe avant d'accèder à une application.
    Comment puis je vérifier sont authentification auprès de l'AD.
    Merci pour votre aide précieuse !!

  2. #2
    Membre expérimenté Avatar de LaChips
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    1 109
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2006
    Messages : 1 109
    Points : 1 482
    Points
    1 482
    Par défaut
    Bonjour,
    Voilà une méthode pour s'authentifier à un LDAP :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
            /// <summary>
            /// Try to connect Username with password
            /// </summary>
            /// <param name="username">Username to test</param>
            /// <param name="passwd">Username's password</param>
            /// <param name="domain">Domain to connect</param>
            /// <returns>True: Username/Password OK; False: Authentication error</returns>
            public bool IsAuthenticated(string username, string passwd, string domain)
            {
                try
                {
                    DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, username, passwd, AuthenticationTypes.Secure);
                    DirectorySearcher search = new DirectorySearcher(entry);
                    search.Filter = "(objectClass=user)";
                    search.SearchScope = SearchScope.Subtree;
                    SearchResult result = search.FindOne();
     
                    foreach (ResultPropertyValueCollection var in result.Properties.Values)
                    {
                        foreach (object var2 in var)
                        {
                            Console.WriteLine(var2.ToString());
                        }
     
                    }
     
                    return (result != null);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error authenticating user. " + ex.Message);
                }
            }

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Décembre 2010
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    Citation Envoyé par LaChips Voir le message
    Bonjour,
    Voilà une méthode pour s'authentifier à un LDAP :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
            /// <summary>
            /// Try to connect Username with password
            /// </summary>
            /// <param name="username">Username to test</param>
            /// <param name="passwd">Username's password</param>
            /// <param name="domain">Domain to connect</param>
            /// <returns>True: Username/Password OK; False: Authentication error</returns>
            public bool IsAuthenticated(string username, string passwd, string domain)
            {
                try
                {
                    DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, username, passwd, AuthenticationTypes.Secure);
                    DirectorySearcher search = new DirectorySearcher(entry);
                    search.Filter = "(objectClass=user)";
                    search.SearchScope = SearchScope.Subtree;
                    SearchResult result = search.FindOne();
     
                    foreach (ResultPropertyValueCollection var in result.Properties.Values)
                    {
                        foreach (object var2 in var)
                        {
                            Console.WriteLine(var2.ToString());
                        }
     
                    }
     
                    return (result != null);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error authenticating user. " + ex.Message);
                }
            }
    Bonjour,

    Quelqu'un peut m'expliquer pourquoi, dans beaucoup de code concernant LDAP/Active Directory en C#, on effectue une authentification :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, username, passwd, AuthenticationTypes.Secure);
    Et ensuite un listing :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
                    DirectorySearcher search = new DirectorySearcher(entry);
                    search.Filter = "(objectClass=user)";
                    search.SearchScope = SearchScope.Subtree;
                    SearchResult result = search.FindOne();
     
                    foreach (ResultPropertyValueCollection var in result.Properties.Values)
                    {
                        foreach (object var2 in var)
                        {
                            Console.WriteLine(var2.ToString());
                        }
     
                    }
    Alors qu'une authentification en soit c'est fournir, un Password ou un mot de passe si ils sont bons --> Authentifié, sinon Non authentifié (Exception).

    Merci.

  4. #4
    Modérateur
    Avatar de DotNetMatt
    Homme Profil pro
    CTO
    Inscrit en
    Février 2010
    Messages
    3 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : CTO
    Secteur : Finance

    Informations forums :
    Inscription : Février 2010
    Messages : 3 611
    Points : 9 742
    Points
    9 742
    Billets dans le blog
    3
    Par défaut
    A priori c'est juste pour verifier visuellement qu'on a bien ete authentifie comme il faut. Tu n'es pas oblige de garder cette partie du code dans ton application si ca ne te sert a rien.

Discussions similaires

  1. [Joomla!] Authentification Active Directory / LDAP automatique
    Par leglen38 dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 7
    Dernier message: 26/11/2010, 15h09
  2. authentification Active directory
    Par joe0302 dans le forum Sécurité
    Réponses: 0
    Dernier message: 11/09/2009, 00h46
  3. authentification active directory asp.net
    Par mpoys dans le forum ASP.NET
    Réponses: 0
    Dernier message: 26/02/2009, 16h46
  4. Authentification Active Directory avec JCIFS
    Par dev_gahie dans le forum Sécurité
    Réponses: 5
    Dernier message: 22/09/2008, 15h37
  5. Active Directory : Ajouter un user dans dossier "Users"
    Par marcusien dans le forum Windows Forms
    Réponses: 2
    Dernier message: 04/09/2007, 18h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo