public Tuple <ulong, ulong?, string> logar(Login login) { username = username.ToUpperInvariant(); List <Usuario> listaUsuario = new List <Usuario>(1); try { using (Conexao.GetInstance) { Conexao.Abrir(); ICRUD <Usuario> ControllerUsuario = new CtrlUsuario(Conexao.GetInstance); listaUsuario = ControllerUsuario.Listar().ToList(); Conexao.Fechar(); } } catch (Exception ex) { return(new Tuple <ulong, ulong?, string>(0, 0, ex.ToString())); } foreach (Usuario usuario in listaUsuario) { string curStoredUsername = usuario.Login.ToUpperInvariant(); storedPassword = usuario.Senha; if (curStoredUsername == username) { //OBS: IdUsuario e IdAcesso deveriam ser do tipo 'ulong' e 'ulong?', não 'int' e 'int?'. id = (ulong)usuario.IdUsuario; idAcesso = (ulong)usuario.IdAcesso; validouSenha = CryptoHash.Verify(storedPassword, password); } else { CryptoHash.Verify(storedPassword, password); } } if (validouSenha) { return(new Tuple <ulong, ulong?, string>(id, idAcesso, username)); } return(new Tuple <ulong, ulong?, string>(0, 0, null)); }
public string adicionarUsuario(Registrar registrar) { email = email.ToLowerInvariant(); username = username.ToUpperInvariant(); Regex regex = new Regex("[^A-Z0-9_]"); Regex emailRegex = new Regex("[^a-z0-9_.@]"); string regexedUsername = regex.Replace(username, String.Empty); usuario.Login = regexedUsername; string regexedEmail = emailRegex.Replace(email, String.Empty); if (regexedEmail != email) { return("Email inválido. Tente novamente." + email + regexedEmail); } List <Usuario> listaUsuario = new List <Usuario>(1); try { using (Conexao.GetInstance) { Conexao.Abrir(); string curStoredUsername = usuario.Login.ToUpperInvariant(); string curStoredEmail = usuario.Email.ToLowerInvariant(); ICRUD <Usuario> ControllerUsuario = new CtrlUsuario(Conexao.GetInstance); listaUsuario = ControllerUsuario.Listar().ToList(); foreach (Usuario usuario in listaUsuario) { curStoredUsername = usuario.Login.ToUpperInvariant(); curStoredEmail = usuario.Email.ToLowerInvariant(); if (curStoredUsername == username || curStoredEmail == email) { //OBS: IdUsuario e IdAcesso deveriam ser do tipo 'ulong' e 'ulong?', não 'int' e 'int?'. return("Usuário ou email já registrados. Tente novamente."); } } CryptoHash cryptohash = new CryptoHash(password); string cryptoHashStr = cryptohash.GetCryptoHash(cryptohash); usuario.Senha = cryptoHashStr; ControllerUsuario.Criar(usuario); Conexao.Fechar(); if (regexedUsername != username) { return("Usuário " + usuario.Login + " continha caracteres não permitidos e foi modificado para " + regexedUsername + " e registrado com sucesso."); } else { return("Usuário " + usuario.Login + " registrado com sucesso"); } } } catch (Exception ex) { return(ex.Message); } }