July 15, 2009

C# Ldap Login Class

In an earlier post I mentioned that when it comes to Win32 vs Ldap speed for login, Win32 is the clear winner. However, if you don't feel comfortable using Win32 (Interop etc.) and you also don't want to use the built in provider, then this is the class for you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.Protocols;
using System.Net;

namespace SecurityFramework
{
public class LdapLogin
{
public static void VerifyCredentials(string ldapServer, string userName, string domain, string password)
{
if (string.IsNullOrEmpty(ldapServer))
ldapServer = null;

bool useSSL = false;
NetworkCredential credentials = new NetworkCredential(userName, password, domain);

LdapConnection authConnect = new LdapConnection(
new LdapDirectoryIdentifier(ldapServer),
null,
AuthType.Basic
);

authConnect.SessionOptions.SecureSocketLayer = useSSL;
authConnect.SessionOptions.ProtocolVersion = 3;

try
{
authConnect.Bind(credentials);
}
catch (LdapException ex)
{
throw ex;
}
}
}
}

No comments:

Post a Comment