Created
August 12, 2018 01:23
-
-
Save reinaldocoelho/63df86dfe3e021a634b40a9bc004939e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Efetua validação de acesso a um servidor LDAP de acordo com os parâmetros informados. | |
/// </summary> | |
/// <param name="LdapServer">Nome do servidor LDAP. Ex: domain.com </param> | |
/// <param name="LdapPort">Porta de acesso ao LDAP. Ex: 389 (não segura) ou 689 (segura) </param> | |
/// <param name="User">Usuário para conexão ao LDAP.</param> | |
/// <param name="Password">Senha para conexão ao LDAP.</param> | |
/// <param name="DnBase">Base de pesquisa para validação do usuário.</param> | |
/// <param name="IsLdaps">True se é utilizada conexão segura(com certificado), False se não for conexão segura.</param> | |
/// <returns>True se a validação ocorreu com sucesso e False se falhou.</returns> | |
public static Tuple<bool,string> AuthenticateLDAP(string LdapServer, int LdapPort, string User, string Password, string DnBase, bool IsLdaps) | |
{ | |
// See http://support.microsoft.com/kb/218185 for full list of LDAP error codes | |
const int ldapErrorInvalidCredentials = 0x31; | |
var domain = LdapServer; | |
var server = LdapServer + ":" + LdapPort; | |
try | |
{ | |
using (var ldapConnection = new LdapConnection(server)) | |
{ | |
var networkCredential = new NetworkCredential(User, Password, domain); | |
ldapConnection.SessionOptions.SecureSocketLayer = IsLdaps; | |
ldapConnection.AuthType = System.DirectoryServices.Protocols.AuthType.Negotiate; | |
ldapConnection.Bind(networkCredential); | |
// If the bind succeeds, the credentials are valid | |
return new Tuple<bool, string>(true,""); | |
} | |
} | |
catch (LdapException ldapException) | |
{ | |
// Invalid credentials throw an exception with a specific error code | |
if (ldapException.ErrorCode.Equals(ldapErrorInvalidCredentials)) | |
{ | |
return new Tuple<bool, string>(false,"Login ou senha invalidos"); | |
} | |
var newLog = new Log { | |
Date = DateTime.Now, | |
Level = "ERROR", | |
Thread = "0", | |
Message = "Erro na autenticação LDAP. Dados da conexão: "+server, | |
Logger = "EnContact.Models.Authentication", | |
Exception = ldapException.ToString() | |
}; | |
newLog.SaveWithDependencies(); | |
return new Tuple<bool, string>(false, "Falha ao acessar o servidor de autenticação"); | |
} | |
} | |
/// <summary> | |
/// Permite efetuar validação de segurança para Active Directory. | |
/// </summary> | |
/// <param name="Domain">Servidor de domínio.</param> | |
/// <param name="DomainPort">Porta do servidor de domínio.</param> | |
/// <param name="ServiceUser">Usuário para conexão ao AD</param> | |
/// <param name="ServicePassword">Senha para conexão ao AD</param> | |
/// <param name="DnBase">Base de domínio para filtro.</param> | |
/// <param name="IsLdaps">True se é utilizada conexão segura(com certificado), False se não for conexão segura.</param> | |
/// <param name="UserToValidate">Usuário para validação</param> | |
/// <param name="PasswordToValidate">Senha para validação</param> | |
/// <returns>True se a validação foi correta e False se houve falha na validação.</returns> | |
public static bool AuthenticateActiveDirectory(string Domain, int DomainPort, string ServiceUser, string ServicePassword, string DnBase, bool IsLdaps, string UserToValidate, string PasswordToValidate) | |
{ | |
// Verifica regras de segurança, se é LDAP ou LDAPS | |
var contextOptions = ContextOptions.Negotiate; | |
if (IsLdaps) | |
{ | |
contextOptions = ContextOptions.Negotiate | ContextOptions.SecureSocketLayer; | |
} | |
var fullDomain = Domain + ":" + DomainPort; | |
using (var pc = new PrincipalContext(ContextType.Domain, fullDomain, DnBase, contextOptions, ServiceUser, ServicePassword)) | |
{ | |
return pc.ValidateCredentials(UserToValidate, PasswordToValidate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment