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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using iiie.Authentication.Business.JWT;
  2. namespace iiie.Authentication.DBO
  3. {
  4. /// <summary>
  5. /// All editable user fields
  6. /// </summary>
  7. public class UserEdit
  8. {
  9. /// <summary>
  10. /// When getting / setting from / to database: salt and password hash
  11. /// When getting from controller: user plain text password
  12. /// When returning from controller: empty or user token
  13. /// </summary>
  14. public object Private { get; private set; }
  15. /// <summary>
  16. /// Application specific user role(s)
  17. /// </summary>
  18. public int Role { get; set; }
  19. /// <summary>
  20. /// Optionnal user first name
  21. /// </summary>
  22. public string Firstname { get; set; }
  23. /// <summary>
  24. /// Optionnal user last name
  25. /// </summary>
  26. public string Lastname { get; set; }
  27. /// <summary>
  28. /// Get the user password salt from Private
  29. /// </summary>
  30. /// <returns>The salt</returns>
  31. public string GetSalt()
  32. {
  33. return ((string[]) Private)[0];
  34. }
  35. /// <summary>
  36. /// Set a value in the Private stirng array
  37. /// </summary>
  38. /// <param name="text">The value to be set</param>
  39. /// <param name="pos">The position in the array</param>
  40. private void SetArrayText(string text, int pos)
  41. {
  42. if (Private == null || Private.GetType() != typeof(string[]))
  43. Private = new string[2];
  44. ((string[]) Private)[pos] = text;
  45. }
  46. /// <summary>
  47. /// Set the user password salt to Private
  48. /// </summary>
  49. /// <param name="salt">The salt</param>
  50. public void SetSalt(string salt)
  51. {
  52. SetArrayText(salt, 0);
  53. }
  54. /// <summary>
  55. /// Get the password hash from Private
  56. /// </summary>
  57. /// <returns>The hash</returns>
  58. public string GetHash()
  59. {
  60. return ((string[])Private)[1];
  61. }
  62. /// <summary>
  63. /// Compute and set the password hash to Private
  64. /// </summary>
  65. /// <param name="password">The plain text password</param>
  66. public void SetPlainTextHash(string password)
  67. {
  68. SetArrayText(PasswordHash.CreateHash(password), 1);
  69. }
  70. /// <summary>
  71. /// Set the password hash to Private
  72. /// </summary>
  73. /// <param name="hash">The password hash</param>
  74. public void SetHash(string hash)
  75. {
  76. SetArrayText(hash, 1);
  77. }
  78. /// <summary>
  79. /// Get the Private as string
  80. /// </summary>
  81. /// <returns>The string data</returns>
  82. public string GetPlainTextPrivate()
  83. {
  84. return (string) Private;
  85. }
  86. /// <summary>
  87. /// Compute and set the token to Private
  88. /// </summary>
  89. /// <param name="username">The user username</param>
  90. /// <param name="salt">The use rpassword salt</param>
  91. public void SetPlainTextToken(string username, string salt)
  92. {
  93. Private = TokenManager.GetToken(username, salt);
  94. }
  95. }
  96. }