1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

MySQL With User System - In-depth Tutorial From Start to Finish

Discussion in 'Tutorials/Documentations' started by andreasb, Jan 8, 2017.

  1. leejewitt

    leejewitt Member

    Messages:
    31
    Likes Received:
    1
    Joined:
    Apr 2, 2017
    here is my code.

    Code:
    using GTANetworkServer;
    using Insight.Database;
    using Insight.Database.Providers.MySql;
    using MySql.Data.MySqlClient;
    
    using BCr = BCrypt.Net;
    
    namespace NewServer
    {
        public class Main : Script
        {
            private static MySqlConnectionStringBuilder _database;
            private static IUserRepository _userRepository;
    
    
            public Main()
            {
                API.onResourceStart += API_onResourceStart;
            }
    
            private void API_onResourceStart()
            {
                MySqlInsightDbProvider.RegisterProvider();
    
                _database = new MySqlConnectionStringBuilder
                ("server=192.168.1.4;user=gtan;database=_cnrp;port=3306;password=231190;");
    
                _userRepository = _database.Connection().As<IUserRepository>();
            
            }
    
            [Command("ul", GreedyArg = true)]
            public void CMD_UserLogin(Client player, string password)
            {
                UserAccount account = _userRepository.GetAccount(player.name);
    
                bool isPasswordCorrect = BCr.BCrypt.Verify(password, account.Hash);
    
                if (isPasswordCorrect)
                {
                    API.sendChatMessageToPlayer(player, "You´re now logged in!");
                }
                else
                {
                    API.sendChatMessageToPlayer(player, "Wrong Password!");
                }
            }
    
            [Command("ur", GreedyArg = true)]
            public void CMD_UserRegistration(Client player, string password)
            {
        
                {
                    var hash = BCr.BCrypt.HashPassword(password,
                        BCr.BCrypt.GenerateSalt(12));
              
    
                    UserAccount account = new UserAccount
                  
    
              
                    {
    
                        Username = player.name,
                        Hash = hash
                    };
    
                    _userRepository.RegisterAccount(account);
    
                    API.sendChatMessageToPlayer(player, "You´re now Registred!");
                }
            }
    
            public interface IUserRepository
            {
                UserAccount RegisterAccount(UserAccount userAccount);
                UserAccount GetAccount(string name);
            }
    
            public class UserAccount
            {
                public string Username { get; set; }
                public string Hash { get; set; }
            }
        }
    
    }
     
  2. leejewitt

    leejewitt Member

    Messages:
    31
    Likes Received:
    1
    Joined:
    Apr 2, 2017
    i have tried logging in as root user but get this error now

    n by the target of an invocation. ---> MySql.Data.MySqlClient.MySqlException: Un
    known column 'hash' in 'field list'
     
  3. Artiii

    Artiii Member

    Messages:
    6
    Likes Received:
    1
    Joined:
    Mar 7, 2017
    Hey i have a little problem, how i can save things like Money and else?
    i tried but if there 2 people registrated then its add the value for me and the other player in the Database.

    i hope anyone can help me. :X
     
  4. Vikos Taredi

    Vikos Taredi Member

    Messages:
    12
    Likes Received:
    0
    Joined:
    Apr 7, 2017
  5. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Why?
     
  6. Moretti

    Moretti Member

    Messages:
    21
    Likes Received:
    1
    Joined:
    May 5, 2017
    Getting this error:
    Severity Code Description Project File Line Suppression State
    Error CS0246 The type or namespace name 'BCrypt' could not be found (are you missing a using directive or an assembly reference?) Gamemode G:\Documents\GTANetwork\RP Script\Solution1\ClassLibrary1\Main.cs 6 Active

    Line 5:
    Code:
    using BCr = BCrypt.Net;
    
    I have BCrypt installed as per the screenshot.
     
  7. -Andreas

    -Andreas Well-Known Member

    Messages:
    182
    Likes Received:
    29
    Joined:
    Sep 29, 2016
    Its not installed by including that part in your script. You're simply telling the compiler you're using the directory. Did you remember to actually include the .dll file in your server folder? And of course putting it in your meta file as well.
     
  8. Moretti

    Moretti Member

    Messages:
    21
    Likes Received:
    1
    Joined:
    May 5, 2017
    I installed it with the package manager and tried to compile. It didn't and came with that error.
     
  9. Moretti

    Moretti Member

    Messages:
    21
    Likes Received:
    1
    Joined:
    May 5, 2017
    Resolved my issue above, downloaded the wrong BCrypt resource.
     
  10. .cs

    .cs New Member

    Messages:
    4
    Likes Received:
    0
    Joined:
    May 25, 2017
    Hey, nice Tutorial! But i got a Problem.
    My Folder looks like this; https://www.fast-images.de/uploads/281b59ae94e3725e569e.png
    I have added following in the Settings.xml; <resource src="Virtual-Reallife" />

    My Code from Main.cs;
    Code:
    using GTANetworkServer;
    using Insight.Database;
    using Insight.Database.Providers.MySql;
    using MySql.Data.MySqlClient;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using BCr = BCrypt.Net;
    
    namespace Virtual_Reallife
    {
        public class Main : Script
        {
    
            private static MySqlConnectionStringBuilder _database;
            private static IUserRepository _userRepository;
    
            public Main()
            {
                API.onResourceStart += API_onResourceStart;
            }
    
            private void API_onResourceStart()
            {
                MySqlInsightDbProvider.RegisterProvider();
    
                _database = new MySqlConnectionStringBuilder("server=localhost;user=root;database=gtav;port=3306;password=;");
                _userRepository = _database.Connection().As<IUserRepository>();
            }
      
            [Command("ul", GreedyArg = true)]
            public void CMD_UserLogin(Client player, string password)
            {
                UserAccount account = _userRepository.GetAccount(player.name);
    
                bool isPasswordCorrect = BCr.BCrypt.Verify(password, account.Hash);
    
                if(isPasswordCorrect)
                {
                    API.sendChatMessageToPlayer(player, "Du wurdest erfolgreich eingeloggt!");
                }
                else
                {
                    API.sendChatMessageToPlayer(player, "Du hast ein falsches Passwort eingegeben!");
                }
            }
    
            [Command("ur", GreedyArg = true)]
            public void CMD_UserRegistration(Client player, string password)
            {
                var hash = BCr.BCrypt.HashPassword(password, BCr.BCrypt.GenerateSalt(12));
    
                UserAccount account = new UserAccount
                {
                    Username = player.name,
                    Hash = hash
                };
    
                _userRepository.RegisterAccount(account);
    
                API.sendChatMessageToPlayer(player, "Du wurdest erfolgreich registriert!");
            }
        }
    
        public interface IUserRepository
        {
            UserAccount RegisterAccount(UserAccount userAccount);
            UserAccount GetAccount(string name);
        }
    
        public class UserAccount
        {
            public string Username { get; set; }
            public string Hash { get; set; }
        }
    }
    
    Meta.xml;
    Code:
    <meta>
      <info name="Virtual-Reallife" description="MySQL Login/Register - System" author=".cs" type="script" />
    
      <script scr="Main.cs" type="server" lang="csharp" />
     
      <assembly ref="InSight.Database.Configuration.dll" />
      <assembly ref="Insight.Database.dll" />
      <assembly ref="Insight.Database.Providers.Default.dll" />
      <assembly ref="Insight.Database.Providers.MySql.dll" />
      <assembly ref="MySql.Data.dll" />
      <assembly ref="System.Data.dll" />
      <assembly ref="System.Globalization.dll" />
      <assembly ref="System.Configuration.dll" />
      <assembly ref="BCrypt.Net.dll" />
    </meta>
     
  11. Jack

    Jack Member Donator

    Messages:
    32
    Likes Received:
    15
    Joined:
    Jan 3, 2017
    Line 3 of your meta.xml..

    Code:
    <script scr="Main.cs" type="server" lang="csharp" />
    
    Should be

    Code:
    <script src="Main.cs" type="server" lang="csharp" />
    
     
    Last edited by a moderator: Jun 14, 2017
    .cs likes this.
  12. .cs

    .cs New Member

    Messages:
    4
    Likes Received:
    0
    Joined:
    May 25, 2017
    Nice it works!

    I wanna try to edit the Login/Register System.
    How can i ask MySQL, is account registered or not?

    Code:
            private void OnPlayerConnectedHandler(Client player)
            {
                if(...
            }
     
  13. Artiii

    Artiii Member

    Messages:
    6
    Likes Received:
    1
    Joined:
    Mar 7, 2017
    Code:
    UserAccount Acc = _userRepository.GetAccount(player.name);
                if (Acc != null)
               {
                    //Account exist
                }
                else
                {
                    //Account Not exist
                }
    
     
    Last edited by a moderator: Jun 14, 2017
  14. croky

    croky Active Member

    Messages:
    70
    Likes Received:
    3
    Joined:
    Jan 4, 2017
    How do you escape strings with this?
     
  15. Zeta

    Zeta Member

    Messages:
    44
    Likes Received:
    18
    Joined:
    Sep 14, 2016
    What if the procedures needs to return more than 1 var? For example a List? How can i handle it?
     
  16. kova98

    kova98 Member

    Messages:
    29
    Likes Received:
    3
    Joined:
    Feb 28, 2017
    This method is actually terrible for anything more complicated than a simple user account system. It has serious scalability issues which will cause a lot of problems further down the road. Look into Entity Framework.
     
  17. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Please back up your claims, I'm interested to know why :=)
     
  18. kova98

    kova98 Member

    Messages:
    29
    Likes Received:
    3
    Joined:
    Feb 28, 2017
    Namely the "There is already an open DataReader associated with this Connection which must be closed first." error.
    This is a common problem and can be solved by doing some rewriting, as explained here, however I abandoned it in favor of Entity Framework which is a much more elegant solution that's easier to expand upon and won't cause any problems further down the road
     
  19. Zeta

    Zeta Member

    Messages:
    44
    Likes Received:
    18
    Joined:
    Sep 14, 2016
    Is there any tutorial about this?
     
    croky likes this.
  20. Luit

    Luit New Member

    Messages:
    4
    Likes Received:
    0
    Joined:
    Aug 7, 2017
    The issue with this is it's extremely limited later down the line and could cause issues and annoyance. It is however perfect for user login and registration systems.
     

Share This Page