3
votes

I am trying to make a simple authentication system with LDAP in .NET. I was checking some namespaces in .NET and simply make the standart code snippet as below.

DirectoryEntry de = new DirectoryEntry(path,username,password);

DirectorySearcher s = new DirectorySearcher(de);
s.Filter = "(&(cn=" + username2 + "))";

SearchResult result = s.FindOne();
if (result != null) {
    Console.WriteLine("User exists");
} else {
    Console.WriteLine("User does not exist");
}

I have an admin username and password, username and password, which I use to authenticate the client application. I have a second username and password, username2 and password2 that needs to be checked in the LDAP to log in.

username is the admin account and username2 is just an user in LDAP. So how can I check username2's password?

2
Similar to: stackoverflow.com/questions/400872/… I prefer the solution of creating a second directory entry with username2 and userpass2, and then trying to access something in it, and catching an exception to determine if it is invalid - Brandon
@Brandon thanks for your reply. But when I do a similar think, irectoryEntry de1 = new DirectoryEntry(path,username2,password2); I got the invalid username and password error. - Arda

2 Answers

1
votes

A slightly backwards (and clunky) way is to log in as the user and try to retrieve something, then treat an exception as an invalid password:

static bool CheckUser(string userName, string password) 
{
    var adSettings = ConfigurationManager.ConnectionStrings["ActiveDirectory"];
    if (adSettings == null ||
        string.IsNullOrWhiteSpace(adSettings.ConnectionString))
    {
        return false;
    }

    try
    {
        using (var de = new DirectoryEntry(adSettings.ConnectionString, userName, password))
        {
            // This should throw an exception if the password is wrong
            object nativeObject = de.NativeObject;
        }
    }
    catch (DirectoryServicesCOMException)
    {
        // Wrong password
        return false;
    }
    catch (System.Runtime.InteropServices.COMException)
    {
        // Can't connect
        return false;
    }

    return true;
}
0
votes

I have something in VB which might help you out i guess. Was working on this few days ago with my collegue. Do let me know--- Code:

    Dim cookie As HttpCookie = New HttpCookie("username")
    cookie.Value = TextBox1.Text
    cookie.Expires = DateAndTime.Now.AddHours(12)
    Response.Cookies.Add(cookie)
    Dim entry As New DirectoryEntry("LDAP://xyz.com/dc=xyz,dc=com", TextBox1.Text, TextBox2.Text)
    Try
        Dim obj As New Object
        obj = entry.NativeObject
        Dim search As New DirectorySearcher(entry)
        search.Filter = "(SAMAccountName=" + TextBox1.Text + ")"
        search.PropertiesToLoad.Add("cn")
        Dim result As SearchResult
        result = search.FindOne()
        If result.Equals(Nothing) then
            MsgBox("Try Again with valid username")
        Else
            MsgBox("User Found!")
        Response.Redirect("~/Dashboard.aspx")
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub