Guest User

Untitled

a guest
May 3rd, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data.SqlClient;
  6. using System.Configuration;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9.  
  10. namespace LoginApp.DataAccess
  11. {
  12.     public class DataAccessLayer
  13.     {
  14.         static SqlConnection Database1Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Database1"].ToString());
  15.  
  16.         public static bool AddUser(string Username, string Password , string Email, string FirstName, string LastName)
  17.         {
  18.             bool Success = false;
  19.             string Query = string.Format("INSERT INTO [Users] (username, password , email, first_name, last_name) VALUES ('{0}','{1}','{2}','{3}','{4}')", Username,md5EncodePassword(Password), Email, FirstName, LastName);
  20.             SqlCommand SqlCommand = new SqlCommand(Query, Database1Connection);
  21.             Database1Connection.Open();
  22.             SqlDataReader SqlDataReader = SqlCommand.ExecuteReader();
  23.             Database1Connection.Close();
  24.             return true;
  25.         }
  26.  
  27.         public static string md5EncodePassword(string OriginalPassword)
  28.         {
  29.             Byte[] OriginalBytes;
  30.             Byte[] EncodedBytes;
  31.             MD5 md5;
  32.             // Convert the original password to bytes; then create the hash
  33.             md5 = new MD5CryptoServiceProvider();
  34.             OriginalBytes = ASCIIEncoding.Default.GetBytes(OriginalPassword);
  35.             EncodedBytes = md5.ComputeHash(OriginalBytes);
  36.             // Bytes to string
  37.             return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(EncodedBytes), "-", "").ToLower();
  38.  
  39.         }
  40.  
  41.         public static bool UserIsValid(string Username, string Password)
  42.         {
  43.             bool Authenticated = false;
  44.  
  45.             string Query = string.Format("SELECT * FROM [Users] WHERE username = '{0}' AND password = '{1}' ", Username, md5EncodePassword(Password));
  46.  
  47.             SqlCommand SqlCommand = new SqlCommand(Query, Database1Connection);
  48.             Database1Connection.Open();
  49.             SqlDataReader SqlDataReader = SqlCommand.ExecuteReader();
  50.             Authenticated = SqlDataReader.HasRows;
  51.             Database1Connection.Close();
  52.             return Authenticated;
  53.         }
  54.     }
  55. }
Add Comment
Please, Sign In to add comment