public async Task <IActionResult> Index(StudentRegistrationModel vm) { if (!ModelState.IsValid) { return(View(vm)); } try { var result = await this.ApiPostAsync(apiPostStudentUri, vm, "StudentApiClient"); if (!result.IsSuccessStatusCode) { ViewBag.Name = "Unable to Save At the Moment"; return(View(vm)); } else { ViewBag.Status = $"The student name { vm.StudentName } is record created! "; RedirectToAction("studentlist", "student"); // return View(vm); } } catch (Exception ex) { LogSecurity.Warning("Exception on StudentRegistration Post : ", ex.InnerException.ToString()); //return View(vm); } return(View(vm)); //RedirectToAction("studentlist", "student"); }
public static string Encrypt(string text, string sKey) { var des = new DESCryptoServiceProvider(); var inputByteArray = Encoding.Default.GetBytes(text); des.Key = Encoding.ASCII.GetBytes(LogSecurity.GetMD5(sKey).Substring(0, 8)); des.IV = Encoding.ASCII.GetBytes(LogSecurity.GetMD5(sKey).Substring(0, 8)); var ms = new System.IO.MemoryStream(); var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); var ret = new StringBuilder(); foreach (var b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return(ret.ToString()); }
public static string Decrypt(string text, string sKey) { var des = new DESCryptoServiceProvider(); var len = text.Length / 2; var inputByteArray = new byte[len]; int x; for (x = 0; x < len; x++) { var i = Convert.ToInt32(text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } des.Key = Encoding.ASCII.GetBytes(LogSecurity.GetMD5(sKey).Substring(0, 8)); des.IV = Encoding.ASCII.GetBytes(LogSecurity.GetMD5(sKey).Substring(0, 8)); var ms = new System.IO.MemoryStream(); var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return(Encoding.Default.GetString(ms.ToArray())); }
public async Task <IActionResult> CreateStudent(StudentRegistrationModel vm) { Log.Information("Authenticated user making student/secretcreatestudent API call."); ResponseModel <StudentRegistrationModel> response; if (ModelState.IsValid) { response = await _studentModelService.CreateStudent(vm); if (response.ReturnStatus == true) { return(Ok(response)); } else { LogSecurity.Warning("Unauthorized access attempted Create {student}", vm.StudentName); return(BadRequest(response)); } } return(BadRequest(vm)); }
public static void ToLogdel(this DataTable table, string filePath, LogSecurity security = LogSecurity.Unprotected) { try { //null values cannot be processed if (table == null) { return; } //store all characters here var sw = new StringWriter(); //headers sw.Write("###"); for (var i = 0; i < table.Columns.Count; i++) { sw.Write(table.Columns[i]); if (i < table.Columns.Count - 1) { sw.Write("!"); } } sw.Write(sw.NewLine); foreach (DataRow dr in table.Rows) { for (var i = 0; i < table.Columns.Count; i++) { if (!Convert.IsDBNull(dr[i])) { var value = dr[i].ToString().CleanLogDel(); sw.Write(value); } if (i < table.Columns.Count - 1) { sw.Write("!"); } } sw.Write(sw.NewLine); } sw.Close(); var contentToWrite = sw.ToString(); //check if the content needs to be protected with DPAPI if (security == LogSecurity.Protected) { //encrypt the log var provider = new ProtectedString(contentToWrite, ProtectionMode.Encrypt); //replace the plainText contents with the new encrypted contents contentToWrite = provider.ProcessedValue; } //finalise the log file File.WriteAllText(filePath, contentToWrite); } catch (Exception) //catch all exceptions { //ignore it } }