KNM.LDAPHelper 1.1.1
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
- SID Retrieval: Automatically obtains the Windows user’s Security Identifier (SID)
- Global Directory User Lookup: Query Active Directory for any user by SID or username
- Cross-platform: Works on Windows (Active Directory environments); provides safe fallback on other platforms
- Dependency Injection Ready: Register as singleton or scoped for easy integration
Quick Start
1. Registration (Program.cs)
builder.Services.AddSingleton<ILdapUserInfoProvider, LdapUserInfoProvider>();
2. 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($"Not in domain or not a Windows/AD user. Result: {_ldapUserInfo.Result}");
}
}
3. Directory User Lookup Example
public async Task LookupBySid(ILdapUserInfoProvider ldapProvider, string sid)
{
var user = await ldapProvider.FindUserBySsIdAsync(sid); // Query by SID, not sAMAccountName
if (user != null) Console.WriteLine(user.DisplayName);
}
4. Returned Data Models
public class LdapUserInfo
{
public bool LdapUser { get; set; }
public string SsId { get; set; } = string.Empty;
public string? Result { get; set; } // Diagnostic message
}
public class DirectoryUser
{
public string SsId { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Surname { get; set; } = string.Empty;
public string UserName { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public bool AccountEnabled { get; set; }
}
How it Works
- On Windows:
- Automatically discovers AD domain and controller
- Extracts current user’s SID (
WindowsIdentity.User.Value) - Performs LDAP global/query by SID (
objectSid) or username (sAMAccountName) [with correct binary conversion for SID]
- On non-Windows platforms:
- Always sets
LdapUser = falseand fillsResultwith an explanatory message
- Always sets
All logic/configuration is resolved automatically within the provider—no external config needed.
Licensing
Proprietary / Private Repository
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)