1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

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

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

  1. Shareef

    Shareef Member

    Messages:
    47
    Likes Received:
    4
    Joined:
    Jan 31, 2017
    I've managed to fix this error (maybe?) by removing the port part of my string and instead adding :3306 to the server part. Now, I am getting this error.
    [16:33:39] System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'BCrypt.Net, Version=0.1.4141.31969, Culture=neutral, PublicKeyToken=f3bc8f8c31beeb49' or one of its dependencies. The system cannot find the file specified.
    at Newserver.Main.CMD_UserRegistration(Client player, String password)
    --- End of inner exception stack trace ---
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at GTANetworkServer.ScriptingEngine.<>c__DisplayClass26_0.<InvokeVoidMethod>b__0() in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:line 176
    at GTANetworkServer.ScriptingEngine.MainThreadLoop() in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:line 109
     
  2. MrPancakers

    MrPancakers Active Member

    Messages:
    90
    Likes Received:
    19
    Joined:
    Aug 2, 2016
    There's your error, make sure you've put BCrypt.Net.dll inside of your resource folder inside your server.
     
  3. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    anyone have any idea about this error?


    [19:50:39] Connection established: Wor-Barry (Wor-Barry) [192.168.1.166]
    [19:50:48] EXCEPTION IN RESOURCE NewServer INSIDE SCRIPTENGINE Main
    [19:50:48] System.Reflection.TargetInvocationException: Exception has been throw
    n by the target of an invocation. ---> System.InvalidCastException: Object canno
    t be cast from DBNull to other types.
     
  4. Shareef

    Shareef Member

    Messages:
    47
    Likes Received:
    4
    Joined:
    Jan 31, 2017
    Got the error again after redoing the whole tutorial (found out I missed a few steps)
    [19:52:20] System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'BCrypt.Net, Version=0.1.4141.31969, Culture=neutral, PublicKeyToken=f3bc8f8c31beeb49' or one of its dependencies. The system cannot find the file specified.
    at Newserver.Main.CMD_UserRegistration(Client player, String password)
    --- End of inner exception stack trace ---
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at GTANetworkServer.ScriptingEngine.<>c__DisplayClass26_0.<InvokeVoidMethod>b__0() in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:line 176
    at GTANetworkServer.ScriptingEngine.MainThreadLoop() in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:line 109
     
  5. Shareef

    Shareef Member

    Messages:
    47
    Likes Received:
    4
    Joined:
    Jan 31, 2017
    Checked again, all the DLLs are still there.
    New error doesn't say anything about BCrypt though...interesting. I th ink I fixed the mention of BCrypt (possibly) by changing it to .dll instead of .Dll in the XML file but I don't know if that has anything to do with it.

    [19:55:48] EXCEPTION IN RESOURCE Insurgency INSIDE SCRIPTENGINE Main
    [19:55:48] System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
    at Insurgency.Main.CMD_UserRegistration(Client player, String password)
    --- End of inner exception stack trace ---
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at GTANetworkServer.ScriptingEngine.<>c__DisplayClass26_0.<InvokeVoidMethod>b__0() in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:line 176
    at GTANetworkServer.ScriptingEngine.MainThreadLoop() in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:line 109
     
  6. Liquidz

    Liquidz Member

    Messages:
    10
    Likes Received:
    4
    Joined:
    Apr 14, 2017
    Same error here, could it be that he can´t connect to the db or something?
    I don´t think that my code is the failure because it´s the same as in the Guide.
     
  7. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    Am not sure what it is i have tested the mysql connection and works fine. So can't really be that
     
  8. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    That is just me testing by the way am sure it will happen with any user
     
  9. Liquidz

    Liquidz Member

    Messages:
    10
    Likes Received:
    4
    Joined:
    Apr 14, 2017
    After 6 hours i think i solved it for now...
    My Problem was the username of the database user, renamed it to something without numbers and special characters, now it works.
    I´ve tried serveral usernames, but i can´t figure it out what´s wrong with some usernames, some work some don´t work.
     
  10. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    That's weird not sure how the username would effect it unless it was wrong.
    My username password and database
    Is all "cnrp" so not a very complicated one
     
  11. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    did you mange to fix this think i have the same error, Object cannot be cast from DBNull to other types
     
  12. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    i tested using the wrong password and got access denied so must be connecting to the database fine
     
  13. Liquidz

    Liquidz Member

    Messages:
    10
    Likes Received:
    4
    Joined:
    Apr 14, 2017
    Try the Username "rooto", thats somehow one of the names that work.
    I also had access denied when ive entered the wrong password.
     
  14. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    Ok will give that username a try
     
  15. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    has made no difference still getting Object cannot be cast from DBNull to other types
     
  16. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017
    here is the complete error in the console.



    [21:18:21] EXCEPTION IN RESOURCE cnr INSIDE SCRIPTENGINE Main
    [21:18:21] System.Reflection.TargetInvocationException: Exception has been throw
    n by the target of an invocation. ---> System.InvalidCastException: Object canno
    t be cast from DBNull to other types.
    at System.DBNull.System.IConvertible.ToByte(IFormatProvider provider)
    at MySql.Data.MySqlClient.MySqlCommandBuilder.DeriveParameters(MySqlCommand c
    ommand)
    at Insight.Database.Providers.MySql.MySqlInsightDbProvider.DeriveParametersFr
    omStoredProcedure(IDbCommand command)
    at Insight.Database.Providers.InsightDbProvider.<>c__DisplayClass3.<DerivePar
    ameters>b__0(IDbConnection _)
    at Insight.Database.DBConnectionExtensions.ExecuteAndAutoClose[T](IDbConnecti
    on connection, Func`2 getCommand, Func`3 translate, CommandBehavior commandBehav
    ior)
    at Insight.Database.Providers.InsightDbProvider.DeriveParameters(IDbCommand c
    ommand)
    at Insight.Database.CodeGenerator.DbParameterGenerator.CreateClassInputParame
    terGenerator(IDbCommand command, Type type)
    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Fu
    nc`2 valueFactory)
    at Insight.Database.DBCommandExtensions.AddParameters(IDbCommand cmd, Object
    parameters)
    at Insight.Database.DBConnectionExtensions.CreateCommand(IDbConnection connec
    tion, String sql, Object parameters, CommandType commandType, Nullable`1 command
    Timeout, IDbTransaction transaction)
    at Insight.Database.DBConnectionExtensions.<>c__DisplayClass69`1.<Query>b__67
    (IDbConnection c)
    at Insight.Database.DBConnectionExtensions.ExecuteAndAutoClose[T](IDbConnecti
    on connection, Func`2 getCommand, Func`3 translate, CommandBehavior commandBehav
    ior)
    at Insight.Database.DBConnectionExtensions.Query[T](IDbConnection connection,
    String sql, Object parameters, IQueryReader`1 returns, CommandType commandType,
    CommandBehavior commandBehavior, Nullable`1 commandTimeout, IDbTransaction tran
    saction, Object outputParameters)
    at NewServer.Main\.IUserRepository6af20afb-7aab-4c33-a21f-f3a4ab447175.Regist
    erAccount(UserAccount userAccount)
    at NewServer.Main.CMD_UserRegistration(Client player, String password)
    --- End of inner exception stack trace ---
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
    Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
    t[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
    Attr, Binder binder, Object[] parameters, CultureInfo culture)
    at GTANetworkServer.ScriptingEngine.<>c__DisplayClass26_0.<InvokeVoidMethod>b
    __0() in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:line
    176
    at GTANetworkServer.ScriptingEngine.<>c__DisplayClass23_0.<MainThreadLoop>b__
    0(Object <state>) in C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\Resource
    Info.cs:line 94
     
  17. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Perhaps you should upload your code and show us. Sorry that I haven't been of any help here, been busy for quite some time.
     
  18. THXTubbs

    THXTubbs New Member

    Messages:
    1
    Likes Received:
    0
    Joined:
    Mar 31, 2017
    I have the exact same problem been looking all over and haven't found a solution as of yet.

    here is my code:

    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 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=;user=;database=;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, "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 registered!");
            }
     
        }
     
        public interface IUserRepository
        {
            UserAccount RegisterAccount(UserAccount userAccount);
            UserAccount GetAccount(string name);
        }
     
        public class UserAccount
        {
            public string Username { get; set; }
            public string Hash { get; set; }
        }
    }
    FIXED:

    Previously I had the database name and the user as the exact same name, I changed the user and it has worked fine for me now. I added _2 onto the old user.
     
    Last edited: Apr 27, 2017
  19. kova98

    kova98 Member

    Messages:
    27
    Likes Received:
    3
    Joined:
    Feb 28, 2017
    It worked flawlessly on my machine. However, when trying to move it to a dedicated host, I started getting the "Object canno
    t be cast from DBNull to other types." error. Changing the username didn't fix the problem.

    It's really frustrating because the only thing changed in the code is this:
    _database = new MySqlConnectionStringBuilder("server=localhost;user=root;database=database;port=3306;password=;");

    to this:
    _database = new MySqlConnectionStringBuilder("server=localhost;user=db_23859_b;database=db_23859;port=3306;password=pass;");

    Error log: https://hastebin.com/ciwajomalo.css
     
    Last edited: Apr 30, 2017
  20. leejewitt

    leejewitt Member

    Messages:
    29
    Likes Received:
    0
    Joined:
    Apr 2, 2017

    i have the same error all tho it never has actually worked i have tried changing the user name and database name and password but still no luck
     

Share This Page