Advertisement
Guest User

Untitled

a guest
May 31st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. using GTANetworkServer;
  2. using Insight.Database;
  3. using Insight.Database.Providers.MySql;
  4. using MySql.Data.MySqlClient;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using BCr = BCrypt.Net;
  11.  
  12. namespace database
  13. {
  14. public class Main : Script
  15. {
  16.  
  17. private static MySqlConnectionStringBuilder _database;
  18. private static IUserRepository _userRepository;
  19.  
  20.  
  21. public Main()
  22. {
  23. API.onResourceStart += API_onResourceStart;
  24. }
  25.  
  26. private void API_onResourceStart()
  27. {
  28. MySqlInsightDbProvider.RegisterProvider();
  29.  
  30. _database = new MySqlConnectionStringBuilder("server=localhost;user=root;database=generationfive;port=3306;password=MPjCbqdwLXDaijN8;");
  31.  
  32. _userRepository = _database.Connection().As<IUserRepository>();
  33. }
  34.  
  35. [Command("ins", GreedyArg = true)]
  36. public void CMD_UserRegistration(Client player, string password)
  37. {
  38. var hash = BCr.BCrypt.HashPassword(password, BCr.BCrypt.GenerateSalt(12));
  39.  
  40. UserAccount account = new UserAccount
  41. {
  42. Username = player.name,
  43. Hash = hash
  44. };
  45.  
  46. _userRepository.RegisterAccount(account);
  47.  
  48. API.sendChatMessageToPlayer(player, "Vous êtes maintenant enregistré");
  49.  
  50. }
  51.  
  52. [Command("con", GreedyArg = true)]
  53. public void CMD_UserLogin(Client player, string password)
  54. {
  55. UserAccount account = _userRepository.GetAccount(player.name);
  56.  
  57. bool isPasswordCorrect = BCr.BCrypt.Verify(password, account.Hash);
  58.  
  59. if (isPasswordCorrect)
  60. {
  61. API.sendChatMessageToPlayer(player, "Connexion réussie!");
  62. }
  63. else
  64. {
  65. API.sendChatMessageToPlayer(player, "Mauvais mot de passe!");
  66. }
  67. }
  68.  
  69.  
  70.  
  71. public interface IUserRepository
  72. {
  73. UserAccount RegisterAccount(UserAccount userAccount);
  74. UserAccount GetAccount(string name);
  75. }
  76.  
  77. public class UserAccount
  78. {
  79. public string Username { get; set; }
  80. public string Hash { get; set; }
  81. }
  82.  
  83.  
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement