Skip to content

Commit 5e673af

Browse files
authored
Merge pull request #22137 from abpframework/AbpIdentityErrorDescriber
Add custom `IdentityErrorDescriber` for localized error messages.
2 parents 9baafac + 299512d commit 5e673af

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using JetBrains.Annotations;
2+
using Microsoft.AspNetCore.Identity;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Localization;
5+
using Volo.Abp.DependencyInjection;
6+
using Volo.Abp.Identity.Localization;
7+
using Volo.Abp.Localization;
8+
9+
namespace Volo.Abp.Identity;
10+
11+
[Dependency(ServiceLifetime.Scoped, ReplaceServices = true)]
12+
[ExposeServices(typeof(IdentityErrorDescriber))]
13+
public class AbpIdentityErrorDescriber : IdentityErrorDescriber
14+
{
15+
protected IStringLocalizer<IdentityResource> Localizer { get; }
16+
17+
public AbpIdentityErrorDescriber(IStringLocalizer<IdentityResource> localizer)
18+
{
19+
Localizer = localizer;
20+
}
21+
22+
public override IdentityError InvalidUserName([CanBeNull] string userName)
23+
{
24+
using (CultureHelper.Use("en"))
25+
{
26+
return new IdentityError
27+
{
28+
Code = nameof(InvalidUserName),
29+
Description = Localizer["Volo.Abp.Identity:InvalidUserName", userName ?? ""]
30+
};
31+
}
32+
}
33+
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ public AbpIdentityUserValidator_Tests()
1919
Localizer = GetRequiredService<IStringLocalizer<IdentityResource>>();
2020
}
2121

22+
[Fact]
23+
public async Task InvalidUserName_Messages_Test()
24+
{
25+
var user = new IdentityUser(Guid.NewGuid(), "abp 123", "[email protected]");
26+
var identityResult = await _identityUserManager.CreateAsync(user);
27+
identityResult.Succeeded.ShouldBeFalse();
28+
identityResult.Errors.Count().ShouldBe(1);
29+
identityResult.Errors.First().Code.ShouldBe("InvalidUserName");
30+
identityResult.Errors.First().Description.ShouldBe(Localizer["Volo.Abp.Identity:InvalidUserName", "abp 123"]);
31+
}
32+
2233
[Fact]
2334
public async Task Can_Not_Use_Another_Users_Email_As_Your_Username_Test()
2435
{

0 commit comments

Comments
 (0)