Functions.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Linq;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace MyTests
  5. {
  6. public class Functions
  7. {
  8. // Валидация логина и пароля при входе
  9. public static bool IsValidLogAndPass(string login, string password)
  10. {
  11. if (login.Trim() == "" || password.Trim() == "")
  12. return false;
  13. else
  14. return true;
  15. }
  16. // Валидация логина и пароля
  17. public static bool IsLogEqualPass(string login, string password)
  18. {
  19. if (login == password)
  20. return false;
  21. else
  22. return true;
  23. }
  24. // Валидация логина и пароля
  25. public static bool IsValidLength(string str)
  26. {
  27. if (str.Length < 5)
  28. return false;
  29. else
  30. return true;
  31. }
  32. // Проверка на правильность введеных данных при входе
  33. public static bool LoginCheck(string login, string password)
  34. {
  35. //if (cnt.db.Users.Select(item => item.Login + item.Password).Contains(login + Encrypt.GetHash(password)))
  36. return true;
  37. //else
  38. // return false;
  39. }
  40. // Проверка на уникальность логина
  41. public static bool IsLoginAlreadyTaken(string login)
  42. {
  43. return cdb.db.Users.Select(item => item.Login).Contains(login);
  44. }
  45. // Валидация электронной почты
  46. public static bool IsValidEmail(string email)
  47. {
  48. if (System.Text.RegularExpressions.Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
  49. return true;
  50. else
  51. return false;
  52. }
  53. // Проверка на уникальность электронной почты
  54. public static bool IsEmailAlreadyTaken(string Email)
  55. {
  56. return cdb.db.Users.Select(item => item.Email).Contains(Email);
  57. }
  58. // Шифрование пароля
  59. public static string EncryptPassword(string password)
  60. {
  61. using (var hash = SHA1.Create())
  62. {
  63. return string.Concat(hash.ComputeHash(Encoding.UTF8.GetBytes(password)).Select(x => x.ToString("X2")));
  64. }
  65. }
  66. public static int CorrectAnswersCounter(Tests test, Users user)
  67. {
  68. Quest.Answer = cdb.db.Questions.Where(item => item.IdTest == test.IdTest).Select(it => it.Answer).ToArray();
  69. Quest.UserAnswer = cdb.db.Answers.Where(item => item.Users.IdUser == user.IdUser && item.Questions.IdTest == test.IdTest).Select(it => it.Answer).ToArray();
  70. int value = 0;
  71. if (Quest.Answer.Length == Quest.UserAnswer.Length)
  72. for (int i = 0; i < Quest.Answer.Length; i++)
  73. if (Quest.Answer[i] == Quest.UserAnswer[i])
  74. value++;
  75. return value;
  76. }
  77. public static class Quest
  78. {
  79. public static string[] Answer;
  80. public static string[] UserAnswer;
  81. }
  82. }
  83. }