API
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MyCommon.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using UnivateProperties_API.Helpers.Attributes;
  9. using UnivateProperties_API.Model.Timeshare;
  10. namespace UnivateProperties_API.Helpers
  11. {
  12. public static class MyCommon
  13. {
  14. public static bool PostToConsoft(TimeshareWeek week)
  15. {
  16. bool flag = false;
  17. try
  18. {
  19. var search = $"http://www.unipoint-consoft.co.za/nph-srep.exe?cluvbank_test.sch&CLUB=LPA&TYPE=BANK&RESORT={week.ResortCode}&UNIT={week.UnitNumber}&MODULE={week.WeekNumber}&YEAR={week.ArrivalDate.Year}&BANKTO=UV";
  20. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
  21. request.Method = "GET";
  22. WebResponse response = request.GetResponse();
  23. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  24. {
  25. string result = reader.ReadToEnd();
  26. flag = result.ToLower().Contains("success");
  27. }
  28. }
  29. catch (Exception)
  30. {
  31. }
  32. return flag;
  33. }
  34. public static List<string> GetVisibleColumns<T>(T item) where T : new()
  35. {
  36. List<string> list = new List<string>();
  37. var type = item.GetType();
  38. var properties = type.GetProperties();
  39. var visibleProperties = properties.Where(x => x.GetCustomAttributes(false).Any(a => a.GetType() == typeof(VisibleInListViewAttribute) && ((a as VisibleInListViewAttribute).Value as bool?) == true)).ToList();
  40. visibleProperties.ForEach(x => list.Add(x.Name));
  41. return list;
  42. }
  43. public static string TenderUrl { get; set; }
  44. public static string Reservations { get; set; }
  45. public static string ReservationsUserCode { get; set; }
  46. public static string ReservationsPassword { get; set; }
  47. public static DateTime GetDateFromString(string value)
  48. {
  49. if (CheckDateDayFirst(value))
  50. {
  51. return new DateTime(Convert.ToInt32(value.Substring(6, 4)), Convert.ToInt32(value.Substring(3, 2)), Convert.ToInt32(value.Substring(0, 2)));
  52. }
  53. else if (CheckDateYearFirst(value))
  54. {
  55. return Convert.ToDateTime(value);
  56. }
  57. else return DateTime.MinValue;
  58. }
  59. public static bool CheckDateYearFirst(string value)
  60. {
  61. if(value.Length >= 10)
  62. {
  63. value = value.Substring(0, 10);
  64. }
  65. Regex reggie = new Regex(@"^\d{4}-((0[1-9])|(1[012]))-((0[1-9]|[12]\d)|3[01])$");
  66. return reggie.IsMatch(value);
  67. }
  68. public static bool CheckDateDayFirst(string value)
  69. {
  70. if (value.Length >= 10)
  71. {
  72. value = value.Substring(0, 10);
  73. }
  74. Regex reggie = new Regex(@"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$");
  75. return reggie.IsMatch(value);
  76. }
  77. public static bool IsValidEmail(string item)
  78. {
  79. if (!string.IsNullOrEmpty(item))
  80. {
  81. Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
  82. Match match = regex.Match(item);
  83. return match.Success;
  84. }
  85. else return false;
  86. }
  87. public static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
  88. {
  89. if (password == null) throw new ArgumentNullException("password");
  90. if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
  91. using (var hmac = new System.Security.Cryptography.HMACSHA512())
  92. {
  93. passwordSalt = hmac.Key;
  94. passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  95. }
  96. }
  97. public static bool VerifyPasswordHash(string password, byte[] storedHash, byte[] storedSalt)
  98. {
  99. if (password == null) throw new ArgumentNullException("password");
  100. if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
  101. if (storedHash.Length != 64) throw new ArgumentException("Invalid length of password hash (64 bytes expected).", "passwordHash");
  102. if (storedSalt.Length != 128) throw new ArgumentException("Invalid length of password salt (128 bytes expected).", "passwordHash");
  103. using (var hmac = new System.Security.Cryptography.HMACSHA512(storedSalt))
  104. {
  105. var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  106. for (int i = 0; i < computedHash.Length; i++)
  107. {
  108. if (computedHash[i] != storedHash[i]) return false;
  109. }
  110. }
  111. return true;
  112. }
  113. public static string CreateRandomPassword(int length = 6)
  114. {
  115. string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-";
  116. Random random = new Random();
  117. char[] chars = new char[length];
  118. for (int i = 0; i < length; i++)
  119. {
  120. chars[i] = validChars[random.Next(0, validChars.Length)];
  121. }
  122. return new string(chars);
  123. }
  124. public static string DateSuffixed(DateTime date)
  125. {
  126. var dt = date;
  127. string suffix;
  128. List<int> thNumbers = new List<int>() { 11, 12, 13 };
  129. if (thNumbers.Contains(dt.Day))
  130. {
  131. suffix = "th";
  132. }
  133. else if (dt.Day % 10 == 1)
  134. {
  135. suffix = "st";
  136. }
  137. else if (dt.Day % 10 == 2)
  138. {
  139. suffix = "nd";
  140. }
  141. else if (dt.Day % 10 == 3)
  142. {
  143. suffix = "rd";
  144. }
  145. else
  146. {
  147. suffix = "th";
  148. }
  149. return suffix;
  150. }
  151. }
  152. }