public static ResultCode Login(string username, string password, ref string key) { try { Logger.Info("Entering UserManager.Login."); Logger.Info("User logging in."); Logger.Info($"Username: {username}"); Logger.Info($"Key: {key}"); if (string.IsNullOrWhiteSpace(key)) { Logger.Info("Logging in failed. No key."); Logger.Info("Leaving UserManager.Login."); return ResultCode.ServerError; } if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) { Logger.Info("Logging in failed. Wrong username or password."); Logger.Info("Leaving UserManager.Login."); return ResultCode.WrongUsernameOrPassword; } using (var connection = new MySqlConnection(Config.ConnectionString)) { var sql = $@"select * from user where USERNAME = '******';"; var userModel = connection.QueryFirstOrDefault<User>(sql); if (userModel?.Password != password) { Logger.Info("Logging in failed. Wrong username or password."); Logger.Info("Leaving UserManager.Login."); return ResultCode.WrongUsernameOrPassword; } var currentTime = CurrentTime; sql = $@"update user set LAST_ACTIVE_TIME = '{currentTime}' where USERNAME = '******';"; connection.Execute(sql); userModel.LastActiveTime = DateTime.Parse(currentTime); if (CurrentUsers.Any(x => x.Value.Username == username)) { Logger.Info("Already logged in, update key."); key = CurrentUsers.Where(x => x.Value.Username == username).Select(x => x.Key).FirstOrDefault(); } else { CurrentUsers.Add(key, userModel); } Logger.Info("Logging in succeed."); Logger.Info("Leaving UserManager.Login."); return ResultCode.Succeed; } } catch (Exception ex) { Logger.Error("Logging in failed. Server error.", ex); Logger.Info("Leaving UserManager.Login."); return ResultCode.ServerError; } }
public static ResultCode Register(string username, string password, string email) { try { Logger.Info("Entering AccountManager.Register."); Logger.Info("Registering new account."); Logger.Info($"Username: {username}"); Logger.Info($"Email: {email}"); if (!ValidateUserName(username)) { Logger.Info("Invalid username."); Logger.Info("Leaving AccountManager.Register."); return ResultCode.InvalidUsername; } using (var connection = new MySqlConnection(Config.ConnectionString)) { var sql = $@"select USERNAME, PASSWORD, EMAIL from user where USERNAME = '******';"; var user = connection.QueryFirstOrDefault<Register>(sql); if (user != null) { Logger.Info($"Query shouldn't has result. Query: {sql}"); Logger.Info("Leaving AccountManager.Register."); return ResultCode.ConflictingUsername; } sql = $@"insert into user (USERNAME, PASSWORD, LAST_ACTIVE_TIME, EMAIL) values('{username}','{password}','{CurrentTime}','{email}');"; if (connection.Execute(sql) == 0) { Logger.Info($"Executing sql query failed. Query: {sql}"); Logger.Info("Leaving AccountManager.Register."); return ResultCode.Failed; } var token = EncryptHelper.Encrypt(Guid.NewGuid() + username, 1); sql = $@"insert into user_to_activate (USERNAME, TOKEN, CREATE_TIME) values('{username}','{token}','{CurrentTime}');"; if (connection.Execute(sql) == 0) { Logger.Info($"Executing sql query failed. Query: {sql}"); Logger.Info("Leaving AccountManager.Register."); return ResultCode.Failed; } MailHelper.SendMail( "noreply", new[] { email }, subject: ActivateMailSubject, content: string.Format(ActivateMailContent, token)); Logger.Info("Registering succeed."); Logger.Info("Leaving AccountManager.Register."); return ResultCode.Succeed; } } catch (Exception ex) { Logger.Error("Failed in registering. Server error.", ex); Logger.Info("Leaving AccountManager.Register."); return ResultCode.ServerError; } }