Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1.  
  2.  
  3. using System;
  4. using GTANetworkServer;
  5. using Insight.Database;
  6. using Insight.Database.Providers.MySql;
  7. using MySql.Data.MySqlClient;
  8.  
  9. using BCr = BCrypt.Net;
  10.  
  11. namespace NewServer
  12. {
  13.     public class Main : Script
  14.     {
  15.         private static MySqlConnectionStringBuilder _database;
  16.         private static IUserRepository _userRepository;
  17.  
  18.         public Main()
  19.         {
  20.             API.onResourceStart += API_onResourceStart;
  21.         }
  22.  
  23.         private void API_onResourceStart()
  24.         {
  25.             MySqlInsightDbProvider.RegisterProvider();
  26.  
  27.             _database = new MySqlConnectionStringBuilder("server=192.168.1.4;user=cnrp;database=cnrp;port=3306;password=cnrp;");
  28.  
  29.             _userRepository = _database.Connection().As<IUserRepository>();
  30.         }
  31.  
  32.         [Command("ul", GreedyArg = true)]
  33.         public void CMD_UserLogin(Client player, string password)
  34.         {
  35.             UserAccount account = _userRepository.GetAccount(player.name);
  36.  
  37.             bool isPasswordCorrect = BCr.BCrypt.Verify(password, account.Hash);
  38.  
  39.             if (isPasswordCorrect)
  40.             {
  41.                 API.sendChatMessageToPlayer(player, "You´re now logged in!");
  42.             }
  43.             else
  44.             {
  45.                 API.sendChatMessageToPlayer(player, "Wrong Password!");
  46.             }
  47.         }
  48.  
  49.         [Command("ur", GreedyArg = true)]
  50.         public void CMD_UserRegistration(Client player, string password)
  51.         {
  52.  
  53.             {
  54.                 var hash = BCr.BCrypt.HashPassword(password, BCr.BCrypt.GenerateSalt(12));
  55.  
  56.                 UserAccount account = new UserAccount
  57.                 {
  58.                     Username = player.name,
  59.                     Hash = hash
  60.                 };
  61.  
  62.                 _userRepository.RegisterAccount(account);
  63.  
  64.                 API.sendChatMessageToPlayer(player, "You´re now Registred!");
  65.             }
  66.         }
  67.  
  68.         public interface IUserRepository
  69.         {
  70.             UserAccount RegisterAccount(UserAccount userAccount);
  71.             UserAccount GetAccount(string password);
  72.         }
  73.  
  74.         public class UserAccount
  75.         {
  76.             public string Username { get; set; }
  77.             public string Hash { get; set; }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement