LDAP authentiation

Scenario:

LDAP authentiation

Solution:

     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
    using System.DirectoryServices.Protocols;
    using System.Net;
    
    public void AuthenticateUser(string name, string pass)
    {
        using (var ldapCon =
                        new LdapConnection(new LdapDirectoryIdentifier(server, 636)))
        {
            ldapCon.Credential = new NetworkCredential(servUser, servPass);
            ldapCon.AuthType = AuthType.Basic;
    
            var options = ldapCon.SessionOptions;
            options.SecureSocketLayer = true;
            options.ProtocolVersion = 3;
            options.VerifyServerCertificate = (con, cer) => true;
    
            ldapCon.Bind();
    
            var result = (SearchResponse)ldapCon.SendRequest(new SearchRequest(query,
                "samAccountName=" + name, SearchScope.Subtree));
    
            foreach (SearchResultEntry entry in result.Entries)
            {
                ldapCon.Bind(new NetworkCredential(entry.DistinguishedName, pass));
                var userID = entry.Attributes["samaccountname"][0].ToString();
                for (var i = 0; i < entry.Attributes["memberOf"].Count; i++)
                {
                    //permissions
                }
            }
        }
    }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...