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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Configuration;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. using iiie.Authentication.DBO;
  8. using Newtonsoft.Json;
  9. namespace iiie.Authentication.Business
  10. {
  11. public static class AuthProvider
  12. {
  13. /// <summary>
  14. /// The registered user dbo, or null if no user is logged
  15. /// </summary>
  16. public static UserGet UserDbo
  17. {
  18. get
  19. {
  20. var claim = ((ClaimsIdentity)Thread.CurrentPrincipal.Identity).Claims.FirstOrDefault(x => x != null && x.Type == "__userdbo__");
  21. if (claim == null)
  22. return null;
  23. return JsonConvert.DeserializeObject<UserGet>(claim.Value);
  24. }
  25. set
  26. {
  27. var claim = new Claim("__userdbo__", JsonConvert.SerializeObject(value));
  28. ((ClaimsIdentity)Thread.CurrentPrincipal.Identity).AddClaim(claim);
  29. }
  30. }
  31. /// <summary>
  32. /// Check if this is a valid email address
  33. /// </summary>
  34. /// <param name="email">The email to test</param>
  35. /// <returns>True if the email is valid, false otherwise</returns>
  36. public static bool IsValidEmail(string email)
  37. {
  38. try
  39. {
  40. return Regex.IsMatch(email,
  41. @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
  42. @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
  43. RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
  44. }
  45. catch (RegexMatchTimeoutException)
  46. {
  47. return false;
  48. }
  49. }
  50. /// <summary>
  51. /// Get a int from config file, or return the default value
  52. /// </summary>
  53. /// <param name="key">The config file key</param>
  54. /// <param name="def">The default value</param>
  55. /// <returns>The found int</returns>
  56. public static int GetInt(string key, int def)
  57. {
  58. var valueStr = GetString(key, def.ToString());
  59. int value;
  60. if (int.TryParse(valueStr, out value))
  61. return value;
  62. return def;
  63. }
  64. /// <summary>
  65. /// Get a string from config file, or return the default value
  66. /// </summary>
  67. /// <param name="key">The config file key</param>
  68. /// <param name="def">The default value</param>
  69. /// <returns>The found string</returns>
  70. public static string GetString(string key, string def)
  71. {
  72. var value = ConfigurationManager.AppSettings[key];
  73. if (value == null)
  74. return def;
  75. return value;
  76. }
  77. /// <summary>
  78. /// Const value to compute password hash
  79. /// </summary>
  80. public static int GetPasswordSaltByteSize()
  81. {
  82. return GetInt("PasswordSaltByteSize", 42);
  83. }
  84. /// <summary>
  85. /// Const value to compute password hash
  86. /// </summary>
  87. public static int GetPasswordHashByteSize()
  88. {
  89. return GetInt("PasswordHashByteSize", 42);
  90. }
  91. /// <summary>
  92. /// Const value to compute password hash
  93. /// </summary>
  94. public static int GetPasswordIterations()
  95. {
  96. return GetInt("PasswordIterations", 2048);
  97. }
  98. /// <summary>
  99. /// Returns a string used to validate the token.
  100. /// Must be constant. Default is app setting 'ValidatorString'
  101. /// </summary>
  102. /// <returns>The string</returns>
  103. public static string GetValidatorString()
  104. {
  105. return GetString("ValidatorString", "__default__");
  106. }
  107. /// <summary>
  108. /// Returns a string used to sign the token
  109. /// Must be constant. Default is app setting 'CredentialKey'
  110. /// </summary>
  111. /// <returns>The string</returns>
  112. public static string GetCredentialKey()
  113. {
  114. return GetString("CredentialKey", "__default__");
  115. }
  116. }
  117. }