public IActionResult Download(string idEnc) { string idDec = Encryption.SymmetricDecrypt(idEnc); Guid id = Guid.Parse(idDec); IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] addr = ipEntry.AddressList; var sub = _subService.GetSubmission(id); string absolutePath = @"ValuableFiles\" + sub.File; FileStream fs = new FileStream(absolutePath, FileMode.Open, FileAccess.Read); MemoryStream toDownload = new MemoryStream(); fs.CopyTo(toDownload); string email = sub.Email; var member = _memService.GetMember(email); bool pass = Encryption.VerifyData(toDownload, member.PublicKey, sub.Signature); MemoryStream actualFile = Encryption.HybridDecrypt(toDownload, member.PrivateKey); if (pass == true) { _logger.LogInformation("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nPDF downloaded"); return(File(actualFile, "application/octet-stream", Guid.NewGuid() + ".pdf")); } else { _logger.LogError("IP: " + addr[1].ToString() + "\nTime: " + DateTime.Now + "\nUser: "******"\nSomething went wrong"); ModelState.AddModelError("", "Something went wrong"); return(View()); } }
public AssignmentViewmodel GetAssignment(Guid id) { var data = _assignmentsRepository.GetAssignment(id); AssignmentViewmodel assignment = new AssignmentViewmodel(); assignment.Id = data.Id; assignment.Name = data.Name; assignment.Description = data.Description; assignment.Deadline = data.Deadline; assignment.Member = _membersService.GetMember(data.MemberEmail); return(assignment); }
/// <summary> /// Visar en sida för att redigera en medlem /// </summary> /// <param name="id"></param> /// <returns></returns> public IActionResult Edit(int?id) { if (id == null) { return(NotFound()); } var member = _membersService.GetMember(id); if (member == null) { return(NotFound()); } return(View(member)); }
public IActionResult Index() { var list = _assignmentsService.GetAssignments(); List <AssignmentViewmodel> assignments = new List <AssignmentViewmodel>(); if (User.IsInRole("Student")) { foreach (AssignmentViewmodel a in list) { string assignmentIssuer = a.Member.Email; string studentsTeacher = _membersService.GetMember(User.Identity.Name).TeacherEmail; if (a.Member.Email.Equals(_membersService.GetMember(User.Identity.Name).TeacherEmail)) { assignments.Add(a); } } var submissions = _assignmentsService.GetSubmissions(User.Identity.Name); ViewBag.Submissions = submissions; return(View(assignments)); } return(View(list)); }
public IActionResult Index() { try { string email; if (User.IsInRole("teacher")) { email = User.Identity.Name; } else { var member = _membersService.GetMember(User.Identity.Name); email = member.LecturerEmail; } var taskList = _taskService.GetTasks(email); return(View(taskList)); } catch (Exception ex) { _logger.LogError(ex.Message + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name); return(RedirectToAction("Error", "Home")); } }
public IActionResult Index() { string email = User.Identity.Name; var student = _memService.GetMember(email); if (student == null) { var list = _taskService.GetTasks(email); return(View(list)); } else { string teaEmail = student.TeacherEmail; var list = _taskService.GetTasks(teaEmail); return(View(list)); } }
public IActionResult AddStudent(AddStudentModel model) { if (!ModelState.IsValid) { TempData["warning"] = "Invalid"; return(View()); } if (_membersService.GetMember(model.MemberModel.Email) != null) { TempData["warning"] = "Student already exists"; return(RedirectToAction("Index", "Assignments")); } string randomPassword = GenerateRandomPassword(); var user = new ApplicationUser { UserName = model.MemberModel.Email, Email = model.MemberModel.Email }; var result = _userManager.CreateAsync(user, randomPassword); if (result.Result.Succeeded) { _userManager.AddToRoleAsync(user, "Student"); EmailModel em = new EmailModel(); em.Email = "*****@*****.**"; em.To = model.MemberModel.Email; model.EmailModel = em; using (MailMessage mm = new MailMessage(model.EmailModel.Email, model.EmailModel.To)) { mm.Subject = "Login Credentials"; mm.Body = "You have been assigned an account. Username is: " + em.To + " and your password is : " + randomPassword; mm.IsBodyHtml = false; using (SmtpClient smtp = new SmtpClient()) { smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(model.EmailModel.Email, "74bf*XBG^0ga"); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(mm); ViewBag.Message = "Email sent"; } } model.MemberModel.TeacherEmail = User.Identity.Name; Tuple <string, string> keys = CryptographicHelper.GenerateAsymmetricKeys(); model.MemberModel.PublicKey = keys.Item1; model.MemberModel.PrivateKey = keys.Item2; _membersService.AddMember(model.MemberModel); } return(RedirectToAction("Index", "Assignments")); }
//[Authorize] public IActionResult Create(IFormFile file, SubmissionViewModel data, string id) { try { string urlEnc = Encryption.SymmetricDecrypt(id); Guid decId = Guid.Parse(urlEnc); var memId = _membersService.GetMember(User.Identity.Name); if (file != null) { data.task = _tasksService.GetTask(decId); if (data.task.deadline > DateTime.Now) { string uniqueFilename; if (System.IO.Path.GetExtension(file.FileName) == ".pdf" && file.Length < 1048576) { byte[] whiteList = new byte[] { 37, 80, 68, 70 }; if (file != null) { MemoryStream ms = new MemoryStream(); using (var f = file.OpenReadStream()) { byte[] buffer = new byte[4]; f.Read(buffer, 0, 4); for (int i = 0; i < whiteList.Length; i++) { if (whiteList[i] == buffer[i]) { } else { ModelState.AddModelError("file", "Invalid file"); _logger.LogError("Invalid file |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name); return(View()); } } f.Position = 0; uniqueFilename = Guid.NewGuid() + Path.GetExtension(file.FileName); data.file = uniqueFilename; string absolutePath = @"ValuableFiles\" + uniqueFilename; try { file.CopyTo(ms); var encFile = Encryption.HybridEncrypt(ms, memId.PublicKey); data.signature = Encryption.SignData(ms, memId.PrivateKey); System.IO.File.WriteAllBytes(absolutePath, encFile.ToArray()); f.Close(); } catch (Exception ex) { _logger.LogError(ex, "Error happend while saving file |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name); return(View("Error", new ErrorViewModel() { Message = "Error while saving the file. Try again later" })); } } } } else { ModelState.AddModelError("file", "File is not valid or size is greater than 10Mb"); _logger.LogError("File is not valid or size is greater than 10Mb |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name); return(View()); } data.email = HttpContext.User.Identity.Name; _submissionService.AddSubmission(data); TempData["message"] = "Document submitted successfully"; _logger.LogInformation("File uploaded successfully | ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name); return(View()); } else { TempData["error"] = "Deadline date overdue"; _logger.LogError("Deadline date overdue |" + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name); return(View()); } } else { ModelState.AddModelError("file", "Check your input. Operation failed"); return(View(data)); } } catch (Exception ex) { _logger.LogError(ex.Message + " ip: " + GetIpAddress() + " | Timestamp: " + DateTime.Now + " | Email: " + User.Identity.Name); return(RedirectToAction("Error")); } }
public Member GetMember(long?id) { return(_members.GetMember(id.Value)); }
public IActionResult Create(IFormFile file, FileViewModel data, Guid id) { try { data.task = _tasksService.GetTask(id); if (data.task.deadline > DateTime.Now) { if (ModelState.IsValid) { string uniqueFilename; if (System.IO.Path.GetExtension(file.FileName) == ".pdf" && file.Length < 1048576) { //137 80 78 71 13 10 26 10 byte[] whiteList = new byte[] { 37, 80, 68, 70 }; if (file != null) { MemoryStream msIn = new MemoryStream(); using (var f = file.OpenReadStream()) { f.Position = 0; byte[] buffer = new byte[4]; f.Read(buffer, 0, 4); for (int i = 0; i < whiteList.Length; i++) { if (whiteList[i] == buffer[i]) { } else { ModelState.AddModelError("file", "file is not valid and accapteable"); return(View()); } } //...other reading of bytes happening f.Position = 0; // f.CopyTo(msIn); //hybird encrypt //uploading the file //correctness uniqueFilename = Guid.NewGuid() + Path.GetExtension(file.FileName); data.file = uniqueFilename; string absolutePath = @"ValuableFiles\" + uniqueFilename; try { var member = _memberService.GetMember(User.Identity.Name); file.CopyTo(msIn); var encryptedData = Encryption.HybridEncrypt(msIn, member.publicKey); System.IO.File.WriteAllBytes(absolutePath, encryptedData.ToArray()); data.signature = Encryption.SignData(encryptedData, member.privateKey); f.Close(); } catch (Exception ex) { //log _logger.LogError(ex, "Error happend while saving file"); return(View("Error", new ErrorViewModel() { Message = "Error while saving the file. Try again later" })); } } } } else { ModelState.AddModelError("file", "File is not valid and acceptable or size is greater than 10Mb"); return(View()); } //once the file has been inserted successfully in the db data.email = HttpContext.User.Identity.Name; //this is the currently logged in user _fileSerive.AddFile(data); var remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress; _logger.LogInformation("Current user uploading in files section: " + remoteIpAddress + " TimeStamp: " + System.DateTime.Now + " User: "******" File Id: " + data.file); TempData["message"] = "File inserted successfully"; return(View()); } else { ModelState.AddModelError("", "Check your input. Operation failed"); return(View(data)); } } else { TempData["error"] = "File deadline already passes"; return(View()); } } catch (Exception ex) { _logger.LogInformation("File upload not working" + ex); return(RedirectToAction("Error", "home")); } }