KNM.LDAPHelper 1.1.2
KNM.LdapUserInfoHelper
A cross-platform .NET library for detecting the local LDAP/Active Directory user context and retrieving the user’s Security Identifier (SID/S-1-x-x). Made for easy use with Dependency Injection and zero external configuration.
Features
- Automatic Windows AD Discovery: Detects domain and controller from the environment—no configuration required
- DNS-based Discovery on Linux/macOS: Attempts SRV lookup
_ldap._tcp.<domain>using the machine DNS suffix to auto-discover LDAP host and base DN - SID Retrieval: Automatically obtains the Windows user’s Security Identifier (SID) when running on Windows
- Global Directory User Lookup: Query Active Directory / LDAP for any directory object by SID or username, or perform global text search on multiple attributes
- Cross-platform: Works on Windows and provides best-effort DNS-based LDAP discovery on Linux/macOS, with safe fallbacks and diagnostics
- Dependency Injection Ready: Single extension method
AddKnmLdapHelper()to register the provider in any .NET application
Quick Start
1. Installation
dotnet add package KNM.LdapUserInfoHelper
2. Registration (Program.cs)
using KNM.LDAPHelper;
var builder = WebApplication.CreateBuilder(args);
// KNM LDAP Helper registration (singleton, auto-discovery)
builder.Services.AddKnmLdapCollection();
var app = builder.Build();
3. Usage Example
public class MyService
{
private readonly LdapUserInfo _ldapUserInfo;
public MyService(ILdapUserInfoProvider ldapUserInfoProvider)
{
_ldapUserInfo = ldapUserInfoProvider.GetUserInfo();
}
public void PrintLdapUser()
{
if (_ldapUserInfo.LdapUser)
Console.WriteLine($"LDAP/AD user detected. SID: {_ldapUserInfo.SsId}");
else
Console.WriteLine($"No LDAP/AD context detected. Result: {_ldapUserInfo.Result}");
}
}
4. Directory User Lookup Examples
// Lookup by SID (Windows: objectSid, non-Windows: falls back to sAMAccountName)
var user = await ldapProvider.FindUserBySsIdAsync("S-1-5-21-...");
// Global search across multiple attributes
var users = await ldapProvider.FindUsersAsync("john"); // searches name, username, mail, etc.
Data Models
| Property | Type | Description |
|---|---|---|
LdapUser |
bool |
True if valid LDAP/AD environment detected |
SsId |
string |
User Security Identifier (Windows) or empty |
Result |
string? |
Diagnostic message |
DirectoryUser.AccountEnabled |
bool |
True if UAC bit 2 (ACCOUNTDISABLE) is not set |
DirectoryUser.ResourceType |
string |
"User", "Computer", etc. from objectCategory |
Platform Support
| Platform | Auto-Discovery | SID Support | Search |
|---|---|---|---|
| Windows | Domain.GetComputerDomain() |
✅ Full | ✅ Full |
| Linux/macOS | DNS SRV _ldap._tcp.domain |
❌ Fallback username | ✅ Full |
How it Works
Windows: Uses Domain.GetComputerDomain() → picks DC → binds LDAP → gets root DN → reads WindowsIdentity.User
Linux/macOS: hostname -d → DNS SRV lookup → LDAP bind → root DN discovery
All connections use LdapConnection with AuthType.Negotiate and proper binary SID encoding.
Licensing
Proprietary / Private Repository (KoNiMa Internal)
No packages depend on KNM.LDAPHelper.
.NET 10.0
- DnsClient (>= 1.8.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- System.DirectoryServices (>= 10.0.0)
- System.DirectoryServices.Protocols (>= 10.0.0)