API
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MyCommon.cs 4.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using UnivateProperties_API.Helpers.Attributes;
  6. namespace UnivateProperties_API.Helpers
  7. {
  8. public static class MyCommon
  9. {
  10. public static List<string> GetVisibleColumns<T>(T item) where T : new()
  11. {
  12. List<string> list = new List<string>();
  13. var type = item.GetType();
  14. var properties = type.GetProperties();
  15. var visibleProperties = properties.Where(x => x.GetCustomAttributes(false).Any(a => a.GetType() == typeof(VisibleInListViewAttribute) && ((a as VisibleInListViewAttribute).Value as bool?) == true)).ToList();
  16. visibleProperties.ForEach(x => list.Add(x.Name));
  17. return list;
  18. }
  19. public static string TenderUrl { get; set; }
  20. public static DateTime GetDateFromString(string value)
  21. {
  22. if (CheckDateDayFirst(value))
  23. {
  24. return new DateTime(Convert.ToInt32(value.Substring(6, 4)), Convert.ToInt32(value.Substring(3, 2)), Convert.ToInt32(value.Substring(0, 2)));
  25. }
  26. else if (CheckDateYearFirst(value))
  27. {
  28. return Convert.ToDateTime(value);
  29. }
  30. else return DateTime.MinValue;
  31. }
  32. public static bool CheckDateYearFirst(string value)
  33. {
  34. if(value.Length >= 10)
  35. {
  36. value = value.Substring(0, 10);
  37. }
  38. Regex reggie = new Regex(@"^\d{4}-((0[1-9])|(1[012]))-((0[1-9]|[12]\d)|3[01])$");
  39. return reggie.IsMatch(value);
  40. }
  41. public static bool CheckDateDayFirst(string value)
  42. {
  43. if (value.Length >= 10)
  44. {
  45. value = value.Substring(0, 10);
  46. }
  47. 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})$");
  48. return reggie.IsMatch(value);
  49. }
  50. public static bool IsValidEmail(string item)
  51. {
  52. if (!string.IsNullOrEmpty(item))
  53. {
  54. Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
  55. Match match = regex.Match(item);
  56. return match.Success;
  57. }
  58. else return false;
  59. }
  60. public static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
  61. {
  62. if (password == null) throw new ArgumentNullException("password");
  63. if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
  64. using (var hmac = new System.Security.Cryptography.HMACSHA512())
  65. {
  66. passwordSalt = hmac.Key;
  67. passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  68. }
  69. }
  70. public static bool VerifyPasswordHash(string password, byte[] storedHash, byte[] storedSalt)
  71. {
  72. if (password == null) throw new ArgumentNullException("password");
  73. if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
  74. if (storedHash.Length != 64) throw new ArgumentException("Invalid length of password hash (64 bytes expected).", "passwordHash");
  75. if (storedSalt.Length != 128) throw new ArgumentException("Invalid length of password salt (128 bytes expected).", "passwordHash");
  76. using (var hmac = new System.Security.Cryptography.HMACSHA512(storedSalt))
  77. {
  78. var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  79. for (int i = 0; i < computedHash.Length; i++)
  80. {
  81. if (computedHash[i] != storedHash[i]) return false;
  82. }
  83. }
  84. return true;
  85. }
  86. }
  87. }