IdentityResultExtensions.cs 5.2 KB

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