From b8207426d05d81663c02f93ffc54433ce643233c Mon Sep 17 00:00:00 2001 From: maliming Date: Sun, 14 Feb 2021 20:59:06 +0800 Subject: [PATCH 1/2] Ignore password validation in IdentityDataSeeder. Resolve #7677 --- .../Volo/Abp/Identity/IdentityDataSeeder.cs | 77 +++++++++---------- .../Volo/Abp/Identity/IdentityUserManager.cs | 11 +++ 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs index 9051dc7d66..851676b840 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs @@ -53,57 +53,54 @@ namespace Volo.Abp.Identity { await IdentityOptions.SetAsync(); - using (IdentityOptions.Value.Password.ClearRequirements()) + var result = new IdentityDataSeedResult(); + //"admin" user + const string adminUserName = "admin"; + var adminUser = await UserRepository.FindByNormalizedUserNameAsync( + LookupNormalizer.NormalizeName(adminUserName) + ); + + if (adminUser != null) { - var result = new IdentityDataSeedResult(); - //"admin" user - const string adminUserName = "admin"; - var adminUser = await UserRepository.FindByNormalizedUserNameAsync( - LookupNormalizer.NormalizeName(adminUserName) - ); + return result; + } - if (adminUser != null) - { - return result; - } + adminUser = new IdentityUser( + GuidGenerator.Create(), + adminUserName, + adminEmail, + tenantId + ) + { + Name = adminUserName + }; + + (await UserManager.CreateAsync(adminUser, adminPassword, validatePassword: false)).CheckErrors(); + result.CreatedAdminUser = true; - adminUser = new IdentityUser( + //"admin" role + const string adminRoleName = "admin"; + var adminRole = + await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName)); + if (adminRole == null) + { + adminRole = new IdentityRole( GuidGenerator.Create(), - adminUserName, - adminEmail, + adminRoleName, tenantId ) { - Name = adminUserName + IsStatic = true, + IsPublic = true }; - (await UserManager.CreateAsync(adminUser, adminPassword)).CheckErrors(); - result.CreatedAdminUser = true; - - //"admin" role - const string adminRoleName = "admin"; - var adminRole = - await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName)); - if (adminRole == null) - { - adminRole = new IdentityRole( - GuidGenerator.Create(), - adminRoleName, - tenantId - ) - { - IsStatic = true, - IsPublic = true - }; - - (await RoleManager.CreateAsync(adminRole)).CheckErrors(); - result.CreatedAdminRole = true; - } + (await RoleManager.CreateAsync(adminRole)).CheckErrors(); + result.CreatedAdminRole = true; + } - (await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors(); + (await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors(); - return result; - } + return result; } } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index 69376958b7..6450adfad9 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -60,6 +60,17 @@ namespace Volo.Abp.Identity CancellationTokenProvider = cancellationTokenProvider; } + public virtual async Task CreateAsync(IdentityUser user, string password, bool validatePassword) + { + var result = await CreateAsync(user); + if (!result.Succeeded) + { + return result; + } + + return await UpdatePasswordHash(user, password, validatePassword); + } + public virtual async Task GetByIdAsync(Guid id) { var user = await Store.FindByIdAsync(id.ToString(), CancellationToken); From 4d829820bbcaaf36b52bdb069a09860d80c54fe6 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 15 Feb 2021 17:29:44 +0800 Subject: [PATCH 2/2] UpdatePasswordHash first when create an user & remove PasswordOptionsExtensions. --- .../Volo/Abp/Identity/IdentityUserManager.cs | 4 +-- .../Abp/Identity/PasswordOptionsExtensions.cs | 35 ------------------- 2 files changed, 2 insertions(+), 37 deletions(-) delete mode 100644 modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/PasswordOptionsExtensions.cs diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index 6450adfad9..508728eea1 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -62,13 +62,13 @@ namespace Volo.Abp.Identity public virtual async Task CreateAsync(IdentityUser user, string password, bool validatePassword) { - var result = await CreateAsync(user); + var result = await UpdatePasswordHash(user, password, validatePassword); if (!result.Succeeded) { return result; } - return await UpdatePasswordHash(user, password, validatePassword); + return await CreateAsync(user); } public virtual async Task GetByIdAsync(Guid id) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/PasswordOptionsExtensions.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/PasswordOptionsExtensions.cs deleted file mode 100644 index 793cc6349e..0000000000 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/PasswordOptionsExtensions.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using Microsoft.AspNetCore.Identity; - -namespace Volo.Abp.Identity -{ - public static class PasswordOptionsExtensions - { - public static IDisposable ClearRequirements(this PasswordOptions options) - { - var oldRequireDigit = options.RequireDigit; - var oldRequiredLength = options.RequiredLength; - var oldRequireLowercase = options.RequireLowercase; - var oldRequireUppercase = options.RequireUppercase; - var oldRequiredUniqueChars = options.RequiredUniqueChars; - var oldRequireNonAlphanumeric = options.RequireNonAlphanumeric; - - options.RequireDigit = false; - options.RequiredLength = 1; - options.RequireLowercase = false; - options.RequireUppercase = false; - options.RequiredUniqueChars = 1; - options.RequireNonAlphanumeric = false; - - return new DisposeAction(() => - { - options.RequireDigit = oldRequireDigit; - options.RequiredLength = oldRequiredLength; - options.RequireLowercase = oldRequireLowercase; - options.RequireUppercase = oldRequireUppercase; - options.RequiredUniqueChars = oldRequiredUniqueChars; - options.RequireNonAlphanumeric = oldRequireNonAlphanumeric; - }); - } - } -}