This is a simple post to demonstrate the username and password validation process from LDAP server in ASP.NET and ASP.NET Core using C#.
ASP.NET
We need to add System.DirectoryServices
DLL reference in our project. In packages.config file, we can add the below package or install it using NuGet.
<packages>
<package id="System.DirectoryServices" version="4.7.0" targetFramework="net461" />
</packages>
Here is the manager class, Validate(string userId, string password)
method will validate things from LDAP server.
using System.DirectoryServices;
namespace DotNet
{
public interface ILdapValidator
{
bool Validate(string userId, string password);
}
public class LdapManager : ILdapValidator
{
public readonly string DomainName;
public readonly int PortNumber;
public LdapManager(string domainName, int port = 389)
{
DomainName = domainName;
PortNumber = port;
}
public bool Validate(string userId, string password)
{
try
{
string path = LdapPath();
string username = UserFullId(userId);
DirectoryEntry de = new DirectoryEntry
(path, username, password, AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(de);
ds.FindOne();
return true;
}
catch (DirectoryServicesCOMException ex)
{
return false;
}
}
public string UserFullId(string userId)
{
string value = string.Format(@"{0}@{1}", userId, DomainName);
return value;
}
public string LdapPath()
{
string value = string.Format(@"LDAP://{0}:{1}", DomainName, PortNumber);
return value;
}
}
}
Here, we are using the LDAP manager class to validate username and password:
string domain = "LdapdomainNameOrIp.com";
int port = 389;
string user = "user.name";
string password = "password@123";
bool isValied = new LdapManager(domain, port).Validate(user, password);
ASP.NET Core
We need to add Novell.Directory.Ldap
DLL reference in our project. In .csproj file, we can add the below package or install it from NuGet.
<ItemGroup>
<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="2.3.8" />
</ItemGroup>
Here is the manager class, Validate(string userId, string password)
method will validate things from LDAP server.
using Novell.Directory.Ldap;
using System;
namespace DotNetCore
{
public interface ILdapValidator
{
bool Validate(string userId, string password);
}
public class LdapManager : ILdapValidator
{
public readonly string DomainName;
public readonly int PortNumber;
public LdapManager(string domainName, int port = 389)
{
DomainName = domainName;
PortNumber = port;
}
public bool Validate(string userId, string password)
{
try
{
string username = UserFullId(userId);
using (var connection = new LdapConnection { SecureSocketLayer = false })
{
connection.Connect(DomainName, PortNumber);
connection.Bind(username, password);
return connection.Bound;
}
}
catch (LdapException ex)
{
return false;
}
}
public string UserFullId(string userId)
{
string value = string.Format(@"{0}@{1}", userId, DomainName);
return value;
}
}
}
Here, we are using the LDAP manager class to validate username and password:
string domain = "LdapdomainNameOrIp.com";
int port = 389;
string user = "user.name";
string password = "password@123";
bool isValied = new LdapManager(domain, port).Validate(user, password);
Source Code
It is a Visual Studio 2017 solution with console projects:
- DotNet: .NET Framework 4.6.1
- DotNetCore: .NET Core 2.2
References
Limitations
- The LDAP path may not be simple as mine so fix it as needed.
- The code may throw an error for untested inputs, if may please let me know
History
- 15th July, 2020: Initial version