How to Authenticate LDAP in .NET

后端 未结 3 1625
甜味超标
甜味超标 2020-12-29 08:42

I would like to authenticate username and passwords for my application on a windows operating system with any directory service. For example it could be microsoft active di

3条回答
  •  粉色の甜心
    2020-12-29 09:35

    I'm not sure I entirely understand the question, but in some situations I've found it easy to authenticate a user by simply doing a search for their account and using their credentials as the username and password.

    A successful query means everything provided was correct, not finding the account means something was wrong.

    //use the users credentials for the query
    DirectoryEntry root = new DirectoryEntry(
        "LDAP://dc=domain,dc=com", 
        loginUser, 
        loginPassword
        );
    
    //query for the username provided
    DirectorySearcher searcher = new DirectorySearcher(
        root, 
        "(sAMAccountName=" + loginUser + ")"
        );    
    
    //a success means the password was right
    bool success = false; 
    try {
        searcher.FindOne();
        success = true;
    }
    catch {
        success = false;
    }
    

    Probably not "best practice", but might get around your issue you are having...

提交回复
热议问题