Functions.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. public static string EncryptPassword(string password)
  59. {
  60. using (var hash = SHA1.Create())
  61. {
  62. return string.Concat(hash.ComputeHash(Encoding.UTF8.GetBytes(password)).Select(x => x.ToString("X2")));
  63. }
  64. }
  65. }
  66. }