public async void TreeMonDbContext_Context_Insert_Async() { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); int res = await context.InsertAsync <User>(_userAsync); Assert.IsTrue(res > 0); }
public async Task <bool> InsertAsync(LogEntry l) { if (UseFileLogging) { _fileLog.LogFile(PathToFileLog); Exception ex = _fileLog.Write( l.LogDate.ToString() + "," + l.Level + "," + l.StackTrace + "," + l.Source + "," + l.InnerException); if (ex == null) { return(true); } else { return(false); } } using (var context = new TreeMonDbContext(_dbConnectionKey)) { return(await context.InsertAsync <LogEntry>(l) > 0 ? true : false); } }
public async Task <ServiceResult> InsertUserAsync(User u, string ipAddress = "", bool ignoreAuthorization = true) { if (u == null) { return(ServiceResponse.Error("Invalid user object.")); } if (!ignoreAuthorization) //we ignore authorization when a new user is registering. { if (!this.DataAccessAuthorized(u, "POST", false)) { return(ServiceResponse.Error("You are not authorized this action.")); } } User dbU; using (var context = new TreeMonDbContext(this._connectionKey)) { dbU = context.GetAll <User>().FirstOrDefault(wu => (wu.Name?.EqualsIgnoreCase(u.Name) ?? false)); if (dbU != null) { return(ServiceResponse.Error("Username already exists.")); } u.UUID = Guid.NewGuid().ToString("N"); u.UUIDType = "User"; u.DateCreated = DateTime.UtcNow; u.LastActivityDate = DateTime.UtcNow; if (await context.InsertAsync <User>(u) > 0) { return(ServiceResponse.OK("", u)); } } return(ServiceResponse.Error("Database failed to save user information.")); }
/// <summary> /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="ip"></param> /// <returns></returns> public async Task <ServiceResult> AuthenticateUserAsync(string userName, string password, string ip) { if (string.IsNullOrWhiteSpace(userName)) { using (var context = new TreeMonDbContext(this._connectionKey)) { await context.InsertAsync <AuthenticationLog>(new AuthenticationLog() { Authenticated = false, AuthenticationDate = DateTime.UtcNow, IPAddress = ip, FailType = "Blank Username", UserName = userName, UUIDType = "AuthenticationLog", Vector = "UserManager.AuthenticateUser" }); } return(ServiceResponse.Error("Invalid username or password.")); } User user = null; try { using (var context = new TreeMonDbContext(this._connectionKey)) { user = (await context.GetAllAsync <User>()).FirstOrDefault(uw => (uw.Name?.EqualsIgnoreCase(userName) ?? false)); if (user == null) { return(ServiceResponse.Error("Invalid username or password.")); } user.LastActivityDate = DateTime.UtcNow; if (!PasswordHash.ValidatePassword(password, user.PasswordHashIterations + ":" + user.PasswordSalt + ":" + user.Password)) { context.Insert <AuthenticationLog>(new AuthenticationLog() { Authenticated = false, AuthenticationDate = DateTime.UtcNow, IPAddress = ip, FailType = "Invalid Password", UserName = userName, UUIDType = "AuthenticationLog", Vector = "UserManager.AuthenticateUser" }); user.FailedPasswordAttemptCount++; context.Update <User>(user); return(ServiceResponse.Error("Invalid username or password.")); } ServiceResult sr = IsUserAuthorized(user, ip); if (sr.Status == "ERROR") { return(sr); } user.Online = true; user.LastLoginDate = DateTime.UtcNow; await context.UpdateAsync <User>(user); } } catch (Exception ex) { _logger.InsertError(ex.Message, "UserManager", "AuthenticateUserAsync:" + userName + " IP:" + ip); return(ServiceResponse.Error(ex.Message)); } return(ServiceResponse.OK()); }