IdentityResultExtensions.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Abp.Collections.Extensions;
  2. using Abp.Localization;
  3. using Abp.Localization.Sources;
  4. using Abp.Text;
  5. using Abp.UI;
  6. using Microsoft.AspNetCore.Identity;
  7. namespace VberZero.IdentityFramework;
  8. public static class IdentityResultExtensions
  9. {
  10. private static readonly Dictionary<string, string> IdentityLocalizations
  11. = new Dictionary<string, string>
  12. {
  13. {"Optimistic concurrency failure, object has been modified.", "Identity.ConcurrencyFailure"},
  14. {"An unknown failure has occurred.", "Identity.DefaultError"},
  15. {"Email '{0}' is already taken.", "Identity.DuplicateEmail"},
  16. {"Role name '{0}' is already taken.", "Identity.DuplicateRoleName"},
  17. {"User name '{0}' is already taken.", "Identity.DuplicateUserName"},
  18. {"Phone Number '{0}' is already taken.", "Identity.DuplicatePhoneNumber"},
  19. {"Email '{0}' is invalid.", "Identity.InvalidEmail"},
  20. {"The provided PasswordHasherCompatibilityMode is invalid.", "Identity.InvalidPasswordHasherCompatibilityMode"},
  21. {"The iteration count must be a positive integer.", "Identity.InvalidPasswordHasherIterationCount"},
  22. {"Role name '{0}' is invalid.", "Identity.InvalidRoleName"},
  23. {"Invalid token.", "Identity.InvalidToken"},
  24. {"User name '{0}' is invalid, can only contain letters or digits.", "Identity.InvalidUserName"},
  25. {"A user with this login already exists.", "Identity.LoginAlreadyAssociated"},
  26. {"Incorrect password.", "Identity.PasswordMismatch"},
  27. {"Passwords must have at least one digit ('0'-'9').", "Identity.PasswordRequireDigit"},
  28. {"Passwords must have at least one lowercase ('a'-'z').", "Identity.PasswordRequireLower"},
  29. {"Passwords must have at least one non alphanumeric character.", "Identity.PasswordRequireNonAlphanumeric"},
  30. {"Passwords must have at least one uppercase ('A'-'Z').", "Identity.PasswordRequireUpper"},
  31. {"Passwords must be at least {0} characters.", "Identity.PasswordTooShort"},
  32. {"Role {0} does not exist.", "Identity.RoleNotFound"},
  33. {"User already has a password set.", "Identity.UserAlreadyHasPassword"},
  34. {"User already in role '{0}'.", "Identity.UserAlreadyInRole"},
  35. {"User is locked out.", "Identity.UserLockedOut"},
  36. {"Lockout is not enabled for this user.", "Identity.UserLockoutNotEnabled"},
  37. {"User {0} does not exist.", "Identity.UserNameNotFound"},
  38. {"User is not in role '{0}'.", "Identity.UserNotInRole"}
  39. };
  40. /// <summary>
  41. /// Checks errors of given <see cref="IdentityResult"/> and throws <see cref="UserFriendlyException"/> if it's not succeeded.
  42. /// </summary>
  43. /// <param name="identityResult">Identity result to check</param>
  44. public static void CheckErrors(this IdentityResult identityResult)
  45. {
  46. if (identityResult.Succeeded)
  47. {
  48. return;
  49. }
  50. throw new UserFriendlyException(identityResult.Errors.Select(err => err.Description).JoinAsString(", "));
  51. }
  52. /// <summary>
  53. /// Checks errors of given <see cref="IdentityResult"/> and throws <see cref="UserFriendlyException"/> if it's not succeeded.
  54. /// </summary>
  55. /// <param name="identityResult">Identity result to check</param>
  56. /// <param name="localizationManager">Localization manager to localize error messages</param>
  57. public static void CheckErrors(this IdentityResult identityResult, ILocalizationManager localizationManager)
  58. {
  59. if (identityResult.Succeeded)
  60. {
  61. return;
  62. }
  63. throw new UserFriendlyException(identityResult.LocalizeErrors(localizationManager));
  64. }
  65. public static string LocalizeErrors(this IdentityResult identityResult, ILocalizationManager localizationManager)
  66. {
  67. return LocalizeErrors(identityResult, localizationManager, VzConsts.LocalizationSourceName);
  68. }
  69. public static string LocalizeErrors(this IdentityResult identityResult, ILocalizationManager localizationManager, string localizationSourceName)
  70. {
  71. if (identityResult.Succeeded)
  72. {
  73. throw new ArgumentException("identityResult.Succeeded should be false in order to localize errors.");
  74. }
  75. if (identityResult.Errors == null)
  76. {
  77. throw new ArgumentException("identityResult.Errors should not be null.");
  78. }
  79. var localizationSource = localizationManager.GetSource(localizationSourceName);
  80. return identityResult.Errors.Select(err => LocalizeErrorMessage(err.Description, localizationSource)).JoinAsString(" ");
  81. }
  82. private static string LocalizeErrorMessage(string identityErrorMessage, ILocalizationSource localizationSource)
  83. {
  84. foreach (var identityLocalization in IdentityLocalizations)
  85. {
  86. if (FormattedStringValueExtracter.IsMatch(identityErrorMessage, identityLocalization.Key, out string[] values))
  87. {
  88. return localizationSource.GetString(identityLocalization.Value, values.Cast<object>().ToArray());
  89. }
  90. }
  91. return localizationSource.GetString("Identity.DefaultError");
  92. }
  93. }