Browse Source

Merge pull request #22137 from abpframework/AbpIdentityErrorDescriber

Add custom `IdentityErrorDescriber` for localized error messages.
pull/22140/head
Alper Ebiçoğlu 12 months ago
committed by GitHub
parent
commit
5e673af7cf
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 33
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentityErrorDescriber.cs
  2. 11
      modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/AbpIdentityUserValidator_Tests.cs

33
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentityErrorDescriber.cs

@ -0,0 +1,33 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity.Localization;
using Volo.Abp.Localization;
namespace Volo.Abp.Identity;
[Dependency(ServiceLifetime.Scoped, ReplaceServices = true)]
[ExposeServices(typeof(IdentityErrorDescriber))]
public class AbpIdentityErrorDescriber : IdentityErrorDescriber
{
protected IStringLocalizer<IdentityResource> Localizer { get; }
public AbpIdentityErrorDescriber(IStringLocalizer<IdentityResource> localizer)
{
Localizer = localizer;
}
public override IdentityError InvalidUserName([CanBeNull] string userName)
{
using (CultureHelper.Use("en"))
{
return new IdentityError
{
Code = nameof(InvalidUserName),
Description = Localizer["Volo.Abp.Identity:InvalidUserName", userName ?? ""]
};
}
}
}

11
modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/AbpIdentityUserValidator_Tests.cs

@ -19,6 +19,17 @@ public class AbpIdentityUserValidator_Tests : AbpIdentityAspNetCoreTestBase
Localizer = GetRequiredService<IStringLocalizer<IdentityResource>>();
}
[Fact]
public async Task InvalidUserName_Messages_Test()
{
var user = new IdentityUser(Guid.NewGuid(), "abp 123", "user@volosoft.com");
var identityResult = await _identityUserManager.CreateAsync(user);
identityResult.Succeeded.ShouldBeFalse();
identityResult.Errors.Count().ShouldBe(1);
identityResult.Errors.First().Code.ShouldBe("InvalidUserName");
identityResult.Errors.First().Description.ShouldBe(Localizer["Volo.Abp.Identity:InvalidUserName", "abp 123"]);
}
[Fact]
public async Task Can_Not_Use_Another_Users_Email_As_Your_Username_Test()
{

Loading…
Cancel
Save