public IActionResult Register(RegisterViewModel user) { if (ModelState.IsValid) { var userList = _context.users.Where(p => p.email == user.register_email).FirstOrDefault(); if (userList != null) { if (user.register_email == userList.email) { ModelState.AddModelError("register_email", "email exists"); return(View("index")); } } PasswordHasher <RegisterViewModel> Hasher = new PasswordHasher <RegisterViewModel>(); user.register_password = Hasher.HashPassword(user, user.register_password); users User = new users() { first_name = user.first_name, last_name = user.last_name, email = user.register_email, password = user.register_password, created_at = DateTime.Now, updated_at = DateTime.Now }; _context.Add(User); _context.SaveChanges(); HttpContext.Session.SetInt32("Id", (int)User.id); return(RedirectToAction("Secrets")); } else { return(View("index")); } }
public IActionResult GetByToken(string token) { var id = _context.SecretItems .FirstOrDefault(b => b.Token == token); try { var item = _context.SecretItems.Find(id.Id); var secretValue = item.Value; _context.SecretItems.Remove(item); _context.SaveChanges(); return(Ok(_protector.Unprotect(secretValue))); } catch (NullReferenceException) { return(NotFound()); } catch (CryptographicException) { return(Ok("This secret has expired")); } }
private void Cleanup(object state) { foreach (var item in _secretContext.SecretItems) { if (item.ExpiredBy < DateTime.Now) { _secretContext.SecretItems.Remove(item); _secretContext.SaveChanges(); } } foreach (var upload in _uploadContext.UploadItems) { if (upload.ExpiredBy < DateTime.Now) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads", upload.Token); _uploadContext.UploadItems.Remove(upload); _uploadContext.SaveChanges(); FileOperator.DeleteUploadedFile(path); } } // Clean-up leftover uploads that have no records in the DB var allUploadedFiles = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads")); foreach (var uploadedFile in allUploadedFiles) { if (Path.GetFileName(uploadedFile) == ".placeholder.txt") { continue; } if (!_uploadContext.UploadItems.Any(b => b.Token == Path.GetFileName(uploadedFile))) { FileOperator.DeleteUploadedFile(uploadedFile); } } }