TShockAPI.DB.User.VerifyPassword C# (CSharp) Method

VerifyPassword() public method

Verifies if a password matches the one stored in the database. If the password is stored in an unsafe hashing algorithm, it will be converted to BCrypt. If the password is stored using BCrypt, it will be re-saved if the work factor in the config is greater than the existing work factor with the new work factor.
public VerifyPassword ( string password ) : bool
password string The password to check against the user object.
return bool
        public bool VerifyPassword(string password)
        {
            try
            {
                if (BCrypt.Net.BCrypt.Verify(password, Password))
                {
                    // If necessary, perform an upgrade to the highest work factor.
                    UpgradePasswordWorkFactor(password);
                    return true;
                }
            }
            catch (SaltParseException)
            {
                if (String.Equals(HashPassword(password), Password, StringComparison.InvariantCultureIgnoreCase))
                {
                    // Return true to keep blank passwords working but don't convert them to bcrypt.
                    if (Password == "non-existant password") {
                        return true;
                    }
                    // The password is not stored using BCrypt; upgrade it to BCrypt immediately
                    UpgradePasswordToBCrypt(password);
                    return true;
                }
                return false;
            }
            return false;
        }

Usage Example

        private void RemoteClient_PacketReceived(Client sender, Client.PacketReceivedEventArgs e)
        {
            PacketType packetType = (PacketType)e.Reader.ReadInt16();

            //Disconnect the user if he attempts to do anything else before authenticating.
            if (packetType != PacketType.Authenticate && !Authenticated)
            {
                Disconnect("Your attempt at sending packets before authenticating has been ignored!");
                return;
            }
            switch (packetType)
            {
                case PacketType.Authenticate:
                    InterfaceType = (InterfaceType)e.Reader.ReadByte();
                    int major = e.Reader.ReadInt32();
                    int minor = e.Reader.ReadInt32();
                    if (Rtc.buildVersion.Major != major || Rtc.buildVersion.Minor != minor)
                    {
                        Disconnect($"Your version ({major}.{minor}) is incompatible with the server's version ({Rtc.buildVersion.Major}.{Rtc.buildVersion.Minor}).");
                        return;
                    }
                    string Username = e.Reader.ReadString();
                    string Password = e.Reader.ReadString();
                    TSUser = TShock.Users.GetUserByName(Username);

                    if (TSUser == null || !TSUser.VerifyPassword(Password))
                    {
                        Disconnect("Invalid username/password or insufficient privileges.");
                        return;
                    }
                    Group g = TShock.Groups.GetGroupByName(TSUser.Group);

                    if (!g.HasPermission("*"))
                    {
                        Disconnect("Invalid username/password or insufficient privileges.");
                        return;
                    }
                    Authenticated = true;
                    Packet pck = new Packet((short)PacketType.MessageBuffer, (short)Rtc.MessagesBuffer.Length);
                    for (int i = 0; i < Rtc.MessagesBuffer.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(Rtc.MessagesBuffer[i]))
                           pck.Write(Rtc.ColorBuffer[i], Rtc.MessagesBuffer[i]);

                    }
                    sender.Send(pck);
                    break;
                case PacketType.Input:
                    string text = e.Reader.ReadString();
                    Rtc.ConsoleInput.SendText(text);
                    break;
            }
        }