C# ASP.NET MVC with Active Directory

saad

New member
Joined
Nov 12, 2024
Messages
1
Programming Experience
Beginner
Hello everyone, i have a problem in my project, for the verification of the password of the users, i try to connect to Active Directory on my project and communicate with him with LDAP request, but it doesnt work, look at my code :
C#:
public IActionResult AuthentifierUtilisateur(string badge, string password)
{
    try
    {
                  
        // Récupère l'utilisateur dans la base de données en utilisant le badge
        var utilisateur = _context.UsersAdBadges
            .Where(u => u.Badge == badge)
            .Select(u => new { u.ad_util, u.Name })
            .FirstOrDefault();

        if (utilisateur == null)
        {
            return Json(new { success = false, message = "Utilisateur non trouvé." });
        }

        // Log du nom d'utilisateur récupéré  et affichage des différentes données nécessaires :
        Console.WriteLine($"-------------------");
        Console.WriteLine($"Utilisateur trouvé : {utilisateur.ad_util}");

        // Connexion au contexte LDAP

        using (var context = new PrincipalContext(
            ContextType.Domain,
            "emea",
            null,
            ContextOptions.Negotiate))


        {
            bool estAuthentifie = context.ValidateCredentials(utilisateur.ad_util, password, ContextOptions.Negotiate);

            if (estAuthentifie)
            {
                return Json(new { success = true, message = "Authentification réussie !" });
            }
            else
            {
                Console.WriteLine("Mot de passe incorrect pour l'utilisateur : " + utilisateur.ad_util);
                return Json(new { success = false, message = "Mot de passe incorrect." });
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Erreur lors de la tentative de connexion LDAP : " + ex.Message);
        return Json(new { success = false, message = "Erreur d'authentification au serveur." });
    }
}

im stuck with this problem for like 5 days, i have to finish this app fastely, can someone help me pls,
 
Last edited by a moderator:
If you have been debugging this for 5 days, then you can provide more details than "it doesn't work". You can tell us what behavior you are seeing. You can tell us which line is not behaving you are expecting it to. You can tell us what else you have tried.
 
Back
Top Bottom