Advertisement
CaldeiraG

Login / Registo MD5

Jun 9th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. LOGIN
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using MySql.Data.MySqlClient;
  12. using System.Security.Cryptography;
  13.  
  14. namespace WindowsFormsApplication6
  15. {
  16.     public partial class Login : Form
  17.     {
  18.         public Login()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  24.         {
  25.             Registro reg = new Registro();
  26.             reg.Show();
  27.             this.Hide();
  28.         }
  29.  
  30.         private void button1_Click(object sender, EventArgs e)
  31.         {
  32.             MD5 md5Hasher = MD5.Create();
  33.             byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(textBox2.Text));
  34.             StringBuilder sBuilder = new StringBuilder();
  35.             for (int i = 0; i < data.Length; i++)
  36.             {
  37.                 sBuilder.Append(data[i].ToString("x2"));
  38.             }
  39.             string cs = "data source=localhost; database=login; user id=root; pwd=''";
  40.             MySqlConnection liga = new MySqlConnection(cs);
  41.             string query = "select * from login where user =?u and pass=?p";
  42.             try
  43.             {
  44.                 MySqlCommand cmd = new MySqlCommand(query, liga);
  45.                 cmd.Parameters.AddWithValue("?u", textBox1.Text);
  46.                 cmd.Parameters.AddWithValue("?p", sBuilder);
  47.                 liga.Open();
  48.                 MySqlDataReader leitor = cmd.ExecuteReader();
  49.                 if (leitor.Read())
  50.                 {
  51.                     MainMenu mm = new MainMenu();
  52.                     mm.Show();
  53.                     this.Hide();
  54.                 }
  55.                 else MessageBox.Show(Convert.ToString(sBuilder));
  56.             }
  57.             catch (MySqlException ex)
  58.             {
  59.                 MessageBox.Show(ex.Message);
  60.             }
  61.  
  62.             finally
  63.             {
  64.                 liga.Close();
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. REGISTO
  71.  
  72. using System;
  73. using System.Collections.Generic;
  74. using System.ComponentModel;
  75. using System.Data;
  76. using System.Drawing;
  77. using System.Linq;
  78. using System.Text;
  79. using System.Threading.Tasks;
  80. using System.Windows.Forms;
  81. using MySql.Data.MySqlClient;
  82. using System.Security.Cryptography;
  83.  
  84. namespace WindowsFormsApplication6
  85. {
  86.     public partial class Registro : Form
  87.     {
  88.         public Registro()
  89.         {
  90.             InitializeComponent();
  91.         }
  92.  
  93.         private void button1_Click(object sender, EventArgs e)
  94.         {
  95.             string cs = "data source=localhost; database=login; user id=root; pwd=''";
  96.             MySqlConnection liga = new MySqlConnection(cs);
  97.             string query = "insert into login(user, pass) values (?u, ?p)";
  98.             DialogResult result = MessageBox.Show("Deseja inserir o registro?", "Registro", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  99.             try
  100.             {
  101.                 MySqlCommand cmd = new MySqlCommand(query, liga);
  102.                 cmd.Parameters.AddWithValue("?u", textBox1.Text);
  103.                 MD5 md5Hasher = MD5.Create();
  104.                 byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(textBox2.Text));
  105.                 StringBuilder sBuilder = new StringBuilder();
  106.                 for (int i = 0; i < data.Length; i++)
  107.                 {
  108.                     sBuilder.Append(data[i].ToString("x2"));
  109.                 }
  110.                 cmd.Parameters.AddWithValue("?p", sBuilder);
  111.                 liga.Open();
  112.                 if(result == DialogResult.OK)
  113.                 {
  114.                     cmd.ExecuteNonQuery();
  115.                     Login login = new Login();
  116.                     login.Show();
  117.                     this.Close();
  118.                    
  119.                 }
  120.             }
  121.  
  122.             catch (MySqlException ex)
  123.             {
  124.                 MessageBox.Show(ex.Message);
  125.             }
  126.  
  127.             finally
  128.             {
  129.                 liga.Close();
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement