API
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MyCommon.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 DateTime GetDateFromString(string value)
  45. {
  46. if (CheckDateDayFirst(value))
  47. {
  48. return new DateTime(Convert.ToInt32(value.Substring(6, 4)), Convert.ToInt32(value.Substring(3, 2)), Convert.ToInt32(value.Substring(0, 2)));
  49. }
  50. else if (CheckDateYearFirst(value))
  51. {
  52. return Convert.ToDateTime(value);
  53. }
  54. else return DateTime.MinValue;
  55. }
  56. public static bool CheckDateYearFirst(string value)
  57. {
  58. if(value.Length >= 10)
  59. {
  60. value = value.Substring(0, 10);
  61. }
  62. Regex reggie = new Regex(@"^\d{4}-((0[1-9])|(1[012]))-((0[1-9]|[12]\d)|3[01])$");
  63. return reggie.IsMatch(value);
  64. }
  65. public static bool CheckDateDayFirst(string value)
  66. {
  67. if (value.Length >= 10)
  68. {
  69. value = value.Substring(0, 10);
  70. }
  71. 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})$");
  72. return reggie.IsMatch(value);
  73. }
  74. public static bool IsValidEmail(string item)
  75. {
  76. if (!string.IsNullOrEmpty(item))
  77. {
  78. Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
  79. Match match = regex.Match(item);
  80. return match.Success;
  81. }
  82. else return false;
  83. }
  84. public static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
  85. {
  86. if (password == null) throw new ArgumentNullException("password");
  87. if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
  88. using (var hmac = new System.Security.Cryptography.HMACSHA512())
  89. {
  90. passwordSalt = hmac.Key;
  91. passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  92. }
  93. }
  94. public static bool VerifyPasswordHash(string password, byte[] storedHash, byte[] storedSalt)
  95. {
  96. if (password == null) throw new ArgumentNullException("password");
  97. if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
  98. if (storedHash.Length != 64) throw new ArgumentException("Invalid length of password hash (64 bytes expected).", "passwordHash");
  99. if (storedSalt.Length != 128) throw new ArgumentException("Invalid length of password salt (128 bytes expected).", "passwordHash");
  100. using (var hmac = new System.Security.Cryptography.HMACSHA512(storedSalt))
  101. {
  102. var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  103. for (int i = 0; i < computedHash.Length; i++)
  104. {
  105. if (computedHash[i] != storedHash[i]) return false;
  106. }
  107. }
  108. return true;
  109. }
  110. public static string CreateRandomPassword(int length = 6)
  111. {
  112. string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-";
  113. Random random = new Random();
  114. char[] chars = new char[length];
  115. for (int i = 0; i < length; i++)
  116. {
  117. chars[i] = validChars[random.Next(0, validChars.Length)];
  118. }
  119. return new string(chars);
  120. }
  121. public static string DateSuffixed(DateTime date)
  122. {
  123. var dt = date;
  124. string suffix;
  125. List<int> thNumbers = new List<int>() { 11, 12, 13 };
  126. if (thNumbers.Contains(dt.Day))
  127. {
  128. suffix = "th";
  129. }
  130. else if (dt.Day % 10 == 1)
  131. {
  132. suffix = "st";
  133. }
  134. else if (dt.Day % 10 == 2)
  135. {
  136. suffix = "nd";
  137. }
  138. else if (dt.Day % 10 == 3)
  139. {
  140. suffix = "rd";
  141. }
  142. else
  143. {
  144. suffix = "th";
  145. }
  146. return suffix;
  147. }
  148. }
  149. }