public RetornoLogin RealizarLogin(string strCPF, string strSenha) { RetornoLogin retLogin = new RetornoLogin(); try { this.strCPF = strCPF; this.strSenha = strSenha; int r = rnd.Next(UserAgents.Length); _UserAgent = UserAgents[r]; if (!Validadores.ValidaCPF(strCPF)) { retLogin.Sucesso = false; retLogin.Mensagem = "CPF Inválido!"; return(retLogin); } Encoding.UTF8.GetBytes("txtNumCpf=" + strCPF + "&txtSenha=" + strSenha + "&btnCancelar=Cancelar&btnAvancar=Avan%E7ar"); byte[] byteArray = Encoding.UTF8.GetBytes("txtNumCpf=" + strCPF + "&txtSenha=" + strSenha); HttpWebRequest requisicao = (HttpWebRequest)HttpWebRequest.Create("https://acesso.serpro.gov.br/HOD10/jsp/logonJava.jsp"); requisicao.UserAgent = _UserAgent; requisicao.CookieContainer = _cookies; requisicao.ContentType = "application/x-www-form-urlencoded"; requisicao.Method = "POST"; requisicao.ContentLength = byteArray.Length; requisicao.Referer = "https://acesso.serpro.gov.br/HOD10/jsp/logonID.jsp"; requisicao.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; requisicao.AllowAutoRedirect = true; Stream streamDados = requisicao.GetRequestStream(); streamDados.Write(byteArray, 0, byteArray.Length); streamDados.Close(); String retornoHtml = ""; using (HttpWebResponse resposta = (HttpWebResponse)requisicao.GetResponse()) { string charset = resposta.CharacterSet; if (charset == "") { charset = "iso-8859-1"; } Encoding responseEncoding = Encoding.GetEncoding(charset); using (streamDados = resposta.GetResponseStream()) { using (StreamReader leitorStream = new StreamReader(streamDados, responseEncoding)) { retornoHtml = leitorStream.ReadToEnd(); int inicio = retornoHtml.IndexOf("top.location.href='"); if (inicio > 0) { inicio += 19; int fim = retornoHtml.IndexOf("';", inicio); retLogin.URL = retornoHtml.Substring(inicio, fim - inicio); urlRefresh = retLogin.URL; retLogin.LU = retLogin.URL.Substring(retLogin.URL.IndexOf("?luid=") + 6); } else { if (retornoHtml.Contains("Senha") && retornoHtml.Contains("confere")) { retLogin.Mensagem = "Usuário/Senha inválido"; retLogin.Sucesso = false; } } } } } if (!String.IsNullOrEmpty(retLogin.URL)) { requisicao = (HttpWebRequest)HttpWebRequest.Create(retLogin.URL); requisicao.UserAgent = _UserAgent; requisicao.CookieContainer = _cookies; requisicao.ContentType = "application/x-www-form-urlencoded"; requisicao.Method = "GET"; requisicao.Referer = "https://acesso.serpro.gov.br/HOD10/jsp/logonID.jsp"; requisicao.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; requisicao.AllowAutoRedirect = true; using (HttpWebResponse resposta = (HttpWebResponse)requisicao.GetResponse()) { string charset = resposta.CharacterSet; if (charset == "") { charset = "iso-8859-1"; } Encoding responseEncoding = Encoding.GetEncoding(charset); using (streamDados = resposta.GetResponseStream()) { using (StreamReader leitorStream = new StreamReader(streamDados, responseEncoding)) { retornoHtml = leitorStream.ReadToEnd(); retLogin.Sucesso = true; if (timer != null && timer.Enabled) { timer.Stop(); } timer.Start(); } } } } else { retLogin.Sucesso = false; } } catch (Exception ex) { log.Error("Erro ao tentar realizar login (loginAcesso.RealizarLogin)", ex); retLogin.Sucesso = false; retLogin.Mensagem = ex.ToString(); } return(retLogin); }
public void DoLogin() { String usuario = ""; String senha = ""; // create the dialog content TextBox content = new TextBox(); content.Text = ""; // create the ModernUI dialog component with the buttons ModernDialog dlg = new ModernDialog { Title = "Nome de Usuário", Content = content, MinHeight = 0, MinWidth = 0, MaxHeight = 480, MaxWidth = 640, }; dlg.OkButton.Content = "Avançar"; dlg.CancelButton.Content = "Cancelar"; dlg.Buttons = new Button[] { dlg.OkButton, dlg.CancelButton }; dlg.OkButton.Click += (object sender, RoutedEventArgs e) => { usuario = content.Text; }; dlg.Activated += (object sender, EventArgs e) => { content.Focus(); }; bool?resultado = dlg.ShowDialog(); if (resultado.HasValue && resultado.Value) { PasswordBox pwdBox = new PasswordBox(); dlg = new ModernDialog { Title = "Senha", Content = pwdBox, MinHeight = 0, MinWidth = 0, MaxHeight = 480, MaxWidth = 640, }; dlg.OkButton.Content = "Avançar"; dlg.CancelButton.Content = "Cancelar"; dlg.Buttons = new Button[] { dlg.OkButton, dlg.CancelButton }; dlg.Activated += (object sender, EventArgs e) => { pwdBox.Focus(); }; dlg.OkButton.Click += (object sender, RoutedEventArgs e) => { senha = pwdBox.Password; }; resultado = dlg.ShowDialog(); if (resultado.HasValue && resultado.Value) { RetornoLogin ret = Login.RealizarLogin(usuario, senha); if (ret.Sucesso) { Properties.Settings.Default.LUName = ret.LU; this.Terminal.Connect(); this.Console.Focus(); } else { MessageBoxResult retorno = ModernDialog.ShowMessage(ret.Mensagem, "Atenção", MessageBoxButton.OK); DoLogin(); } } } }