Merge pull request #7362 from abpframework/maliming/AbpIdentityResultExtensions

Allows to customize the English localized text of Identity errors.
pull/7470/head
Yunus Emre Kalkan 5 years ago committed by GitHub
commit b6909b62aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,17 +1,50 @@
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Resources;
using Microsoft.Extensions.Localization;
using Volo.Abp;
using Volo.Abp.Identity;
using Volo.Abp.Localization;
using Volo.Abp.Text.Formatting;
namespace Microsoft.AspNetCore.Identity
{
public static class AbpIdentityResultExtensions
{
private static readonly Dictionary<string, string> IdentityStrings = new Dictionary<string, string>();
static AbpIdentityResultExtensions()
{
var identityResourceManager = new ResourceManager("Microsoft.Extensions.Identity.Core.Resources", typeof(UserManager<>).Assembly);
var resourceSet = identityResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, false);
if (resourceSet == null)
{
throw new AbpException("Can't get the ResourceSet of Identity.");
}
var iterator = resourceSet.GetEnumerator();
while (true)
{
if (!iterator.MoveNext())
{
break;
}
var key = iterator.Key?.ToString();
var value = iterator.Value?.ToString();
if (key != null && value != null)
{
IdentityStrings.Add(key, value);
}
}
if (!IdentityStrings.Any())
{
throw new AbpException("ResourceSet values of Identity is empty.");
}
}
public static void CheckErrors(this IdentityResult identityResult)
{
if (identityResult.Succeeded)
@ -41,25 +74,19 @@ namespace Microsoft.AspNetCore.Identity
}
var error = identityResult.Errors.First();
var key = $"Volo.Abp.Identity:{error.Code}";
var englishString = IdentityStrings.GetOrDefault(error.Code);
using (CultureHelper.Use(CultureInfo.GetCultureInfo("en")))
if (englishString == null)
{
var englishLocalizedString = localizer[key];
if (englishLocalizedString.ResourceNotFound)
{
return Array.Empty<string>();
}
if (FormattedStringValueExtracter.IsMatch(error.Description, englishLocalizedString.Value,
out var values))
{
return values;
}
return Array.Empty<string>();
}
if (FormattedStringValueExtracter.IsMatch(error.Description, englishString, out var values))
{
return values;
}
return Array.Empty<string>();
}
public static string LocalizeErrors(this IdentityResult identityResult, IStringLocalizer localizer)
@ -85,16 +112,12 @@ namespace Microsoft.AspNetCore.Identity
if (!localizedString.ResourceNotFound)
{
using (CultureHelper.Use(CultureInfo.GetCultureInfo("en")))
var englishString = IdentityStrings.GetOrDefault(error.Code);
if (englishString != null)
{
var englishLocalizedString = localizer[key];
if (!englishLocalizedString.ResourceNotFound)
if (FormattedStringValueExtracter.IsMatch(error.Description, englishString, out var values))
{
if (FormattedStringValueExtracter.IsMatch(error.Description, englishLocalizedString.Value,
out var values))
{
return string.Format(localizedString.Value, values.Cast<object>().ToArray());
}
return string.Format(localizedString.Value, values.Cast<object>().ToArray());
}
}
}

@ -19,6 +19,12 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Volo\Abp\Identity\LocalizationExtensions\*.json" />
<Content Remove="Volo\Abp\Identity\LocalizationExtensions\*.json" />
</ItemGroup>
</Project>

@ -2,9 +2,12 @@
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Identity.Localization;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.Identity;
using Volo.Abp.Threading;
using Volo.Abp.VirtualFileSystem;
namespace Volo.Abp.Identity
{
@ -21,6 +24,18 @@ namespace Volo.Abp.Identity
{
options.AutoEventSelectors.Add<IdentityUser>();
});
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<AbpIdentityDomainTestModule>();
});
Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Get<IdentityResource>()
.AddVirtualJson("/Volo/Abp/Identity/LocalizationExtensions");
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

@ -28,10 +28,18 @@ namespace Volo.Abp.Identity
using (CultureHelper.Use("tr"))
{
var localizeMessage = exception.LocalizeMessage(new LocalizationContext(ServiceProvider));
localizeMessage.ShouldContain("Şifre en az 6 karakter uzunluğunda olmalı.");
localizeMessage.ShouldContain("Şifre en az bir sayı ya da harf olmayan karakter içermeli.");
}
using (CultureHelper.Use("en"))
{
var localizeMessage = exception.LocalizeMessage(new LocalizationContext(ServiceProvider));
localizeMessage.ShouldContain("Password length must be greater than 6 characters.");
localizeMessage.ShouldContain("Password must contain at least one non-alphanumeric character.");
}
}
}
}

@ -0,0 +1,7 @@
{
"culture": "en",
"texts": {
"Volo.Abp.Identity:PasswordTooShort": "Password length must be greater than {0} characters.",
"Volo.Abp.Identity:PasswordRequiresNonAlphanumeric": "Password must contain at least one non-alphanumeric character."
}
}
Loading…
Cancel
Save