Advertisement
Guest User

Login

a guest
Apr 30th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace beadando_i7vs1c
  13. {
  14.     public partial class LogInForm : Form
  15.     {
  16.         private DataClasses1DataContext _db;
  17.  
  18.         public LogInForm()
  19.         {
  20.             InitializeComponent();
  21.             _db = new DataClasses1DataContext();
  22.         }
  23.  
  24.         private void LogInForm_FormClosed(object sender, FormClosedEventArgs e)
  25.         {
  26.             WelcomeForm wf = new WelcomeForm();
  27.             wf.Show();
  28.         }
  29.  
  30.         private void btBack_Click(object sender, EventArgs e)
  31.         {
  32.             this.Close();
  33.         }
  34.  
  35.         private void button1_Click(object sender, EventArgs e)
  36.         {
  37.             if (String.IsNullOrWhiteSpace(tbUsername.Text))
  38.             {
  39.                 MessageBox.Show("Nem adott meg felhasználónevet.");
  40.                 return;
  41.             }
  42.             if (String.IsNullOrWhiteSpace(tbPassword.Text))
  43.             {
  44.                 MessageBox.Show("Nem adott meg jelszót.");
  45.                 return;
  46.             }
  47.             string username = this.tbUsername.Text;
  48.             string password = this.tbPassword.Text;
  49.             string codedpass = this.EncodePass(password);
  50.             User loginuser = CheckForUserAndPass(username, codedpass);
  51.             if (loginuser == null)
  52.             {
  53.                 MessageBox.Show("Nem megfelelő felhasználónevet vagy jelszót adott meg.");
  54.             }
  55.             else
  56.             {
  57.                 this.Hide();
  58.                 UserWelcome uw = new UserWelcome(loginuser);
  59.                 uw.Show();
  60.             }
  61.         }
  62.  
  63.         private User CheckForUserAndPass(string username, string password)
  64.         {
  65.             try
  66.             {
  67.                 User queryUser = (from un in _db.Users
  68.                                   where un.UserName == username && un.Password == password
  69.                                   select un).First();
  70.                 return queryUser;
  71.             }
  72.             catch (InvalidOperationException)
  73.             {
  74.                 return null;  
  75.             }
  76.         }
  77.  
  78.         private string EncodePass(string password)
  79.         {
  80.             byte[] codedpass = new UTF8Encoding().GetBytes(password);
  81.             byte[] hash = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(codedpass);
  82.             string encoded = BitConverter.ToString(hash).ToLower();
  83.             return encoded;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement