patch ldap

This commit is contained in:
Luis M 2024-04-09 15:06:26 -05:00
parent 3db3ebeec6
commit b5c54b1d8d
2 changed files with 14 additions and 15 deletions

View File

@ -19,13 +19,13 @@ namespace LdapLoginLib.Data
private static string? AdminPassword { get; set; } = null; private static string? AdminPassword { get; set; } = null;
static LdapConfig() internal LdapConfig()
{ {
//string? mode = Environment.GetEnvironmentVariable("Mode"); //string? mode = Environment.GetEnvironmentVariable("Mode");
ReadCredentials(); ReadCredentials();
} }
private static void ReadCredentials() private void ReadCredentials()
{ {
try try
{ {
@ -79,19 +79,18 @@ namespace LdapLoginLib.Data
} }
internal NetworkCredential AdminCredential()
internal static NetworkCredential AdminCredential()
{ {
return new NetworkCredential($"uid={AdminUser},{AdminDn}", AdminPassword); return new NetworkCredential($"uid={AdminUser},{AdminDn}", AdminPassword);
} }
internal static NetworkCredential UserCredentials(string username, string password) internal NetworkCredential UserCredentials(string username, string password)
{ {
return new NetworkCredential($"uid={username},{UserDn}", password); return new NetworkCredential($"uid={username},{UserDn}", password);
} }
internal static LdapConnection Connection() internal LdapConnection Connection()
{ {
return new LdapConnection($"{ServerIP}:{ServerPort}"); return new LdapConnection($"{ServerIP}:{ServerPort}");
} }

View File

@ -6,17 +6,17 @@ namespace LdapLoginLib
{ {
public class LDAP public class LDAP
{ {
private readonly LdapConfig _ldap;// = new(); private static readonly LdapConfig _ldap = new();
public LDAP() //public LDAP()
{ //{
_ldap = new LdapConfig(); // _ldap = new LdapConfig();
} //}
public bool Login(string password, string? document = null, string? username = null, string? email = null) public bool Login(string password, string? document = null, string? username = null, string? email = null)
{ {
using (LdapConnection ldapConnection = LdapConfig.Connection()) using (LdapConnection ldapConnection = _ldap.Connection())
{ {
try try
{ {
@ -31,7 +31,7 @@ namespace LdapLoginLib
// Set LDAP connection options // Set LDAP connection options
ldapConnection.SessionOptions.SecureSocketLayer = false; ldapConnection.SessionOptions.SecureSocketLayer = false;
ldapConnection.AuthType = AuthType.Basic; ldapConnection.AuthType = AuthType.Basic;
ldapConnection.Credential = LdapConfig.UserCredentials(userInfo.Usuario!, password); ldapConnection.Credential = _ldap.UserCredentials(userInfo.Usuario!, password);
// Attempt to bind (authenticate) the user // Attempt to bind (authenticate) the user
ldapConnection.Bind(); ldapConnection.Bind();
@ -88,14 +88,14 @@ namespace LdapLoginLib
#endregion #endregion
using (LdapConnection ldapConnection = LdapConfig.Connection()) using (LdapConnection ldapConnection = _ldap.Connection())
{ {
try try
{ {
// Admin login // Admin login
ldapConnection.SessionOptions.SecureSocketLayer = false; ldapConnection.SessionOptions.SecureSocketLayer = false;
ldapConnection.AuthType = AuthType.Basic; ldapConnection.AuthType = AuthType.Basic;
ldapConnection.Credential = LdapConfig.AdminCredential(); ldapConnection.Credential = _ldap.AdminCredential();
ldapConnection.Bind(); ldapConnection.Bind();