[HttpPost] // query string public async Task <bool> ForgetPasswordUsername(string credentialUsername) { await DatabaseFunctions.InitializeStaticStorage(_db).ConfigureAwait(false); try { credentialUsername = credentialUsername.ToLower(); var credentialFromDb = await _db.Credentials.Where(c => c.CredentialsUsername.Equals(credentialUsername)) .FirstOrDefaultAsync().ConfigureAwait(false); if (credentialFromDb == null) { return(false); } var credentialUniqueId = ModelCreation.GenerateRandomString(); // generate credentialUniqueId credentialFromDb.CredentialChangePasswordId = credentialUniqueId; await _db.SaveChangesAsync(); // send it by email await EmailServices.SendEmail(credentialUsername, $"Verification Code: {credentialUniqueId}"); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
public async Task <string> PostDiagnosticDataFromPc(DiagnosticData diagnosticData) { await DatabaseFunctions.InitializeStaticStorage(_db).ConfigureAwait(false); var admins = diagnosticData.PcConfiguration.Admins; foreach (var admin in admins) { if (!StaticStorageServices.PcMapper.ContainsKey(admin.Item1)) { continue; } //Check Pc Admin Password if (!StaticStorageServices.AdminMapper[admin.Item1].Equals(admin.Item2)) { continue; } //if the admin contains the pc if (StaticStorageServices.PcMapper[admin.Item1].ContainsKey(diagnosticData.PcId)) { StaticStorageServices.PcMapper[admin.Item1][diagnosticData.PcId] = diagnosticData; await DatabaseFunctions.UpdatePcInDatabase(_db, diagnosticData).ConfigureAwait(false); await DatabaseFunctions.UpdatePcLastCurrentSecond(diagnosticData, _db).ConfigureAwait(false); await _db.SaveChangesAsync().ConfigureAwait(false); } else { StaticStorageServices.PcMapper[admin.Item1].Add(diagnosticData.PcId, diagnosticData); var pc = await _db.Pcs.Where(p => p.PcId == diagnosticData.PcId).FirstOrDefaultAsync().ConfigureAwait(false); if (pc != null) { await DatabaseFunctions.UpdatePcInDatabase(_db, diagnosticData).ConfigureAwait(false); await DatabaseFunctions.UpdatePcLastCurrentSecond(diagnosticData, _db).ConfigureAwait(false); } else { var newPc = ModelCreation.CreatePc(diagnosticData); //Task.WaitAll(); await DatabaseFunctions.InitializePcLastMinute(diagnosticData, _db).ConfigureAwait(false); await _db.Pcs.AddAsync(newPc).ConfigureAwait(false); } await DatabaseFunctions.AddPcToAdmin(diagnosticData, admin.Item1, _db).ConfigureAwait(false); await _db.SaveChangesAsync().ConfigureAwait(false); } } return("true"); }
public static async Task ChangePasswordInDb(string userName, string newPassword, PcHealthContext db) { var credentialFromDb = await db.Credentials.Where(c => c.CredentialsUsername.Equals(userName)).FirstOrDefaultAsync(); var(salt, passwordHash) = HashServices.Encrypt(newPassword); credentialFromDb.CredentialsPassword = passwordHash; credentialFromDb.CredentialsSalt = salt; await db.SaveChangesAsync(); }
public static async Task CreateNewAdmin(PcHealthContext dbContext, NewAccountInfo newAccountInfo) { var newAdmin = new Admin() { AdminFirstName = newAccountInfo.AdminFirstName, AdminLastName = newAccountInfo.AdminLastName, AdminCredentialsUsername = newAccountInfo.CredentialsUsername }; await dbContext.Admins.AddAsync(newAdmin).ConfigureAwait(false); await dbContext.SaveChangesAsync().ConfigureAwait(false); }
public static async Task <string> CreateNewCredentials(PcHealthContext dbContext, NewAccountInfo newAccountInfo) { var(salt, passwordHash) = Services.HashServices.Encrypt(newAccountInfo.CredentialsPassword); var pcCredentialsPassword = ModelCreation.GenerateRandomString(); var newCredential = new Credential() { CredentialsUsername = newAccountInfo.CredentialsUsername, CredentialsPassword = passwordHash, CredentialsSalt = salt, PcCredentialPassword = pcCredentialsPassword }; await dbContext.Credentials.AddAsync(newCredential).ConfigureAwait(false); StaticStorageServices.AdminMapper.Add(newAccountInfo.CredentialsUsername, pcCredentialsPassword); await dbContext.SaveChangesAsync().ConfigureAwait(false); return(pcCredentialsPassword); }
public static async Task UpdatePcInDatabase(PcHealthContext db, DiagnosticData diagnosticData) { var pc = await db.Pcs.Where(p => p.PcId.Equals(diagnosticData.PcId)).FirstOrDefaultAsync().ConfigureAwait(false); if (pc != null) { pc.PcCpuUsage = diagnosticData.CpuUsage; pc.PcDiskTotalFreeSpace = diagnosticData.TotalFreeDiskSpace; pc.PcDiskTotalSpace = diagnosticData.DiskTotalSpace; pc.PcFirewallStatus = diagnosticData.FirewallStatus; pc.PcId = diagnosticData.PcId; pc.PcMemoryUsage = diagnosticData.MemoryUsage; pc.PcNetworkAverageBytesReceived = diagnosticData.AvgNetworkBytesReceived; pc.PcNetworkAverageBytesSend = diagnosticData.AvgNetworkBytesSent; pc.PcOs = diagnosticData.Os; pc.PcUsername = diagnosticData.PcConfiguration.PcUsername; pc.PcEmail = diagnosticData.PcConfiguration.PcEmail; await db.SaveChangesAsync(); } }