Advertisement
Guest User

Untitled

a guest
Jan 15th, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using MySql.Data.MySqlClient;
  4. using System.Net.Mail;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7.  
  8. namespace MMO_SERVER
  9. {
  10.     public class Database
  11.     {
  12.         string _dbHostName = "freemysqlhosting.net";
  13.         string _dbName = "";
  14.         string _dbUserName = "";
  15.         string _dbPassword = "*";
  16.         string _dbTableName = "players";
  17.    
  18.         public Queue connectionList = new Queue ();
  19.         public int connectionPoolSize = 10;
  20.  
  21.         MySqlConnection _dbConnection;
  22.         static object locker = new object();
  23.  
  24.         public Database ()
  25.         {
  26.             try
  27.             {
  28.                 for(int i = 0; i < connectionPoolSize; i++)
  29.                 {
  30.                     _dbConnection = new MySqlConnection(
  31.  
  32.                         "server="   + _dbHostName + ";" +
  33.                         "user="     + _dbUserName + ";" +
  34.                         "database=" + _dbName     + ";" +
  35.                         "password=" +_dbPassword  + "; " +
  36.                         "Pooling = false;"
  37.                     );
  38.  
  39.                     _dbConnection.OpenAsync ();
  40.                     connectionList.Enqueue(_dbConnection);
  41.                 }
  42.                    
  43.                 Console.WriteLine("######### DB CONNECTION INFO ##########");
  44.                 Console.WriteLine ("DataBase: {0}", _dbName);
  45.                 Console.WriteLine("State: {0}", _dbConnection.State);
  46.                 Console.WriteLine("Timeout: {0}", _dbConnection.ConnectionTimeout);
  47.                 Console.WriteLine("Created connections: {0}", connectionPoolSize);
  48.                 Console.WriteLine("########################################");
  49.             }
  50.             catch (Exception ex)
  51.             {
  52.                 Console.WriteLine("Error connection to DataBase: " + ex.Message);
  53.             }
  54.                
  55.             //RegisterNewPlayer ("Tonny", "treeetata", "veldwolfs@gmail.com");
  56.             //LoginUser("Tonny", "veldwolfsBAyPL");
  57.             //ChangePasswordAsync("veldwolfs@gmail.com").GetAwaiter();
  58.  
  59.             Console.ReadKey ();
  60.             _dbConnection.Close ();
  61.             Console.WriteLine ("Database connection closed!");
  62.  
  63.         }
  64.  
  65.         public void GetConnnect()
  66.         {
  67.             lock (locker)
  68.             {
  69.                 _dbConnection = (MySqlConnection)connectionList.Dequeue ();
  70.             }
  71.         }
  72.  
  73.         public void ReturnConnect()
  74.         {
  75.             lock (locker)
  76.             {
  77.                 connectionList.Enqueue (_dbConnection);
  78.             }
  79.         }
  80.            
  81.         private bool CheckUserName(string _userName)
  82.         {
  83.             string checkSQL = "SELECT `name` FROM `"+ _dbTableName +"`" +
  84.                 "WHERE `name` = @Name";
  85.  
  86.             MySqlCommand checkCommand = new MySqlCommand (checkSQL, _dbConnection);
  87.  
  88.             checkCommand.Parameters.Add("@Name", MySqlDbType.VarChar, 20);
  89.             checkCommand.Parameters["@Name"].Value = _userName;
  90.  
  91.             string result = (String)checkCommand.ExecuteScalar ();
  92.  
  93.             if (result == _userName)
  94.                 return true;
  95.             else
  96.                 return false;
  97.         }
  98.  
  99.         private bool CheckUserEmail(string _userEmail)
  100.         {
  101.             string checkSQL = "SELECT `email` FROM `"+ _dbTableName +"`" +
  102.                 "WHERE `email` = @Email";
  103.  
  104.             MySqlCommand checkCommand = new MySqlCommand (checkSQL, _dbConnection);
  105.  
  106.             checkCommand.Parameters.Add("@Email", MySqlDbType.VarChar, 30);
  107.             checkCommand.Parameters["@Email"].Value = _userEmail;
  108.  
  109.             string result = (String)checkCommand.ExecuteScalar ();
  110.  
  111.             if (result == _userEmail)
  112.                 return true;
  113.             else
  114.                 return false;
  115.         }
  116.  
  117.         private bool CheckUser(string _userName, string _userPassword)
  118.         {
  119.  
  120.             string checkSQL = "SELECT `name`, `password` FROM `"+ _dbTableName +"`" +
  121.                 "WHERE `name` = @Name";
  122.  
  123.             MySqlCommand checkCommand = new MySqlCommand (checkSQL, _dbConnection);
  124.  
  125.             checkCommand.Parameters.Add("@Name", MySqlDbType.VarChar, 20);
  126.             checkCommand.Parameters["@Name"].Value = _userName;
  127.  
  128.             string _readName = null;
  129.             string _readPass = null;
  130.  
  131.             MySqlDataReader MyDataReader = checkCommand.ExecuteReader();
  132.  
  133.             while (MyDataReader.Read())
  134.             {
  135.                 _readName = MyDataReader.GetString(0);
  136.                 _readPass = MyDataReader.GetString(1);
  137.             }
  138.  
  139.                
  140.             MyDataReader.Close();
  141.  
  142.             if (_readName == _userName && SecurityManager.VerifyHashedPassword(_readPass, _userPassword))
  143.                 return true;
  144.             else
  145.                 return false;
  146.         }
  147.  
  148.         /*
  149.             Сделать на стороне клиета проверку заполняемых полей на наличие пробелов и запрещенных символов,
  150.             наличие в пароле не мение 6 символов
  151.             наличие в имени не мение 3 символов
  152.             наличие @ в емейле
  153.         */
  154.         public void RegisterNewPlayer(string _name, string _password, string _email)
  155.         {
  156.             if (CheckUserName (_name))
  157.                 Console.WriteLine ("User name already registered!");
  158.             else if (CheckUserEmail (_email))
  159.                 Console.WriteLine ("User email already registered!");
  160.             else
  161.             {
  162.                 _password = SecurityManager.HashPassword (_password);
  163.  
  164.                 string registerSQL = "INSERT INTO `"+ _dbTableName +"`(`name`, `password`, `email`) " +
  165.                     "VALUES (@Name, @Password, @Email)";
  166.  
  167.                 MySqlCommand regCommand = new MySqlCommand(registerSQL, _dbConnection);
  168.  
  169.                 regCommand.Parameters.Add("@Name", MySqlDbType.VarChar, 20);
  170.                 regCommand.Parameters["@Name"].Value = _name;
  171.                 regCommand.Parameters.Add("@Password", MySqlDbType.VarChar, 255);
  172.                 regCommand.Parameters["@Password"].Value = _password;
  173.                 regCommand.Parameters.Add("@Email", MySqlDbType.VarChar, 30);
  174.                 regCommand.Parameters["@Email"].Value = _email;
  175.  
  176.                 regCommand.ExecuteNonQuery ();
  177.  
  178.                 Console.WriteLine (string.Format("Registered new player: nickname = {0}, email = {1}", _name, _email));
  179.             }
  180.         }
  181.  
  182.         public void LoginUser(string _name, string _password)
  183.         {
  184.             if (CheckUser (_name, _password))
  185.             {
  186.                 Console.WriteLine ("User: [" + _name + "] join to server!");   
  187.             }
  188.             else
  189.                 Console.WriteLine ("Incorrect name or password!");
  190.         }
  191.  
  192.         private async Task ChangePasswordAsync(string _mail)
  193.         {
  194.             MailAddress from = new MailAddress("v", "");
  195.             MailAddress to = new MailAddress(_mail);
  196.             MailMessage m = new MailMessage(from, to);
  197.             m.Subject = "Reset password";
  198.             m.Body = "Your request on change password is apply. Your new password is: " + SetNewPassword(_mail);
  199.             SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
  200.             smtp.Credentials = new NetworkCredential("", "");
  201.             smtp.EnableSsl = true;
  202.             await smtp.SendMailAsync(m);
  203.             Console.WriteLine("Message send. User [" + _mail + "] changed password!");
  204.         }
  205.  
  206.         private string SetNewPassword(string _user)
  207.         {
  208.             string newPassword = _user.Split("@"[0])[0] + GetPass (5);
  209.  
  210.             string changePasswordSQL = "UPDATE `"+ _dbTableName +"` SET `password` = @Password WHERE `email` = @Email";
  211.  
  212.             MySqlCommand regCommand = new MySqlCommand(changePasswordSQL, _dbConnection);
  213.             regCommand.Parameters.Add("@Password", MySqlDbType.VarChar, 255);
  214.             regCommand.Parameters["@Password"].Value = SecurityManager.HashPassword (newPassword);
  215.             regCommand.Parameters.Add("@Email", MySqlDbType.VarChar, 30);
  216.             regCommand.Parameters["@Email"].Value = _user;
  217.             regCommand.ExecuteNonQuery ();
  218.  
  219.             return newPassword;
  220.         }
  221.  
  222.         private string GetPass(int x)
  223.         {
  224.             string pass = "";
  225.             var r = new Random();
  226.             while (pass.Length < x)
  227.             {
  228.                 Char c = (char)r.Next(33, 125);
  229.                 if (Char.IsLetterOrDigit(c))
  230.                     pass += c;
  231.             }
  232.             return pass;
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement