using Nini.Config; using System.Net; using System.DirectoryServices.Protocols; namespace LdapLoginLib.Data { internal class LdapConfig { // DN Config (route) private static string? UserDn { get; set; } = null; private static string? AdminDn { get; set; } = null; //Server Config private static string? ServerIP { get; set; } = null; private static int? ServerPort { get; set; } = null; // Admin Config private static string? AdminUser { get; set; } = null; private static string? AdminPassword { get; set; } = null; static LdapConfig() { //string? mode = Environment.GetEnvironmentVariable("Mode"); ReadCredentials(); } private static void ReadCredentials() { try { var filePath = Path.Combine("C:\\Users\\Administrator", ".hims", "iop"); //Validacion manual antes de leer if (File.Exists(filePath) == false) //Exist = false { //Devolver error throw new Exception("Error: conexion con el servidor de identidad."); } IConfigSource source = new Nini.Config.IniConfigSource(filePath); var ldap_config = source.Configs["ldap"]; int _port; if (int.TryParse(ldap_config.Get("port"), out _port)) { ServerPort = _port; } ServerIP = ldap_config.Get("server"); UserDn = ldap_config.Get("user_dn"); AdminDn = ldap_config.Get("admin_dn"); AdminUser = ldap_config.Get("admin_user"); AdminPassword = ldap_config.Get("admin_pass"); if (String.IsNullOrEmpty(ServerIP) || ServerPort == null || String.IsNullOrEmpty(UserDn) || String.IsNullOrEmpty(AdminDn) || String.IsNullOrEmpty(AdminUser) || String.IsNullOrEmpty(AdminPassword) ) { throw new Exception("Error interno. Credenciales invalidas."); } } catch (IOException ioEx) { Console.WriteLine(ioEx.Message); throw; } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } } internal static NetworkCredential AdminCredential() { return new NetworkCredential($"uid={AdminUser},{AdminDn}", AdminPassword); } internal static NetworkCredential UserCredentials(string username, string password) { return new NetworkCredential($"uid={username},{UserDn}", password); } internal static LdapConnection Connection() { return new LdapConnection($"{ServerIP}:{ServerPort}"); } } }