Browse Source

Make sure that identityOptions is overwritten before use.

pull/6333/head
maliming 6 years ago
parent
commit
8ff3917fa0
  1. 1
      modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs
  2. 7
      modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs
  3. 2
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs
  4. 4
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs
  5. 3
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs
  6. 7
      modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AccountAppService_Tests.cs
  7. 8
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderBase.cs
  8. 8
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs
  9. 8
      modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/FakeExternalLoginProvider.cs
  10. 7
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs
  11. 6
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs

1
modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs

@ -16,7 +16,6 @@ namespace Volo.Abp.Account
protected IdentityUserManager UserManager { get; } protected IdentityUserManager UserManager { get; }
protected IAccountEmailer AccountEmailer { get; } protected IAccountEmailer AccountEmailer { get; }
protected IdentitySecurityLogManager IdentitySecurityLogManager { get; } protected IdentitySecurityLogManager IdentitySecurityLogManager { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; } protected IOptions<IdentityOptions> IdentityOptions { get; }
public AccountAppService( public AccountAppService(

7
modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs

@ -2,6 +2,7 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Volo.Abp.Account.Localization; using Volo.Abp.Account.Localization;
using Volo.Abp.Account.Settings; using Volo.Abp.Account.Settings;
using Volo.Abp.Account.Web.Areas.Account.Controllers.Models; using Volo.Abp.Account.Web.Areas.Account.Controllers.Models;
@ -27,12 +28,14 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers
protected IdentityUserManager UserManager { get; } protected IdentityUserManager UserManager { get; }
protected ISettingProvider SettingProvider { get; } protected ISettingProvider SettingProvider { get; }
protected IdentitySecurityLogManager IdentitySecurityLogManager { get; } protected IdentitySecurityLogManager IdentitySecurityLogManager { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; }
public AccountController( public AccountController(
SignInManager<IdentityUser> signInManager, SignInManager<IdentityUser> signInManager,
IdentityUserManager userManager, IdentityUserManager userManager,
ISettingProvider settingProvider, ISettingProvider settingProvider,
IdentitySecurityLogManager identitySecurityLogManager) IdentitySecurityLogManager identitySecurityLogManager,
IOptions<IdentityOptions> identityOptions)
{ {
LocalizationResource = typeof(AccountResource); LocalizationResource = typeof(AccountResource);
@ -40,6 +43,7 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers
UserManager = userManager; UserManager = userManager;
SettingProvider = settingProvider; SettingProvider = settingProvider;
IdentitySecurityLogManager = identitySecurityLogManager; IdentitySecurityLogManager = identitySecurityLogManager;
IdentityOptions = identityOptions;
} }
[HttpPost] [HttpPost]
@ -104,6 +108,7 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers
return new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword); return new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword);
} }
await IdentityOptions.SetAsync();
return GetAbpLoginResult(await SignInManager.CheckPasswordSignInAsync(identityUser, login.Password, true)); return GetAbpLoginResult(await SignInManager.CheckPasswordSignInAsync(identityUser, login.Password, true));
} }

2
modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Volo.Abp.Account.Localization; using Volo.Abp.Account.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Abp.Identity; using Volo.Abp.Identity;
@ -16,6 +17,7 @@ namespace Volo.Abp.Account.Web.Pages.Account
public SignInManager<IdentityUser> SignInManager { get; set; } public SignInManager<IdentityUser> SignInManager { get; set; }
public IdentityUserManager UserManager { get; set; } public IdentityUserManager UserManager { get; set; }
public IdentitySecurityLogManager IdentitySecurityLogManager { get; set; } public IdentitySecurityLogManager IdentitySecurityLogManager { get; set; }
public IOptions<IdentityOptions> IdentityOptions { get; set; }
protected AccountPageModel() protected AccountPageModel()
{ {

4
modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs

@ -186,6 +186,8 @@ namespace Volo.Abp.Account.Web.Pages.Account
return RedirectToPage("./Login"); return RedirectToPage("./Login");
} }
await IdentityOptions.SetAsync();
var loginInfo = await SignInManager.GetExternalLoginInfoAsync(); var loginInfo = await SignInManager.GetExternalLoginInfoAsync();
if (loginInfo == null) if (loginInfo == null)
{ {
@ -259,6 +261,8 @@ namespace Volo.Abp.Account.Web.Pages.Account
protected virtual async Task<IdentityUser> CreateExternalUserAsync(ExternalLoginInfo info) protected virtual async Task<IdentityUser> CreateExternalUserAsync(ExternalLoginInfo info)
{ {
await IdentityOptions.SetAsync();
var emailAddress = info.Principal.FindFirstValue(AbpClaimTypes.Email); var emailAddress = info.Principal.FindFirstValue(AbpClaimTypes.Email);
var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id); var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id);

3
modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Account.Settings; using Volo.Abp.Account.Settings;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
using Volo.Abp.Identity; using Volo.Abp.Identity;
@ -124,6 +125,8 @@ namespace Volo.Abp.Account.Web.Pages.Account
protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string emailAddress) protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string emailAddress)
{ {
await IdentityOptions.SetAsync();
var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id); var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id);
(await UserManager.CreateAsync(user)).CheckErrors(); (await UserManager.CreateAsync(user)).CheckErrors();

7
modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AccountAppService_Tests.cs

@ -1,5 +1,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Shouldly; using Shouldly;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Xunit; using Xunit;
@ -12,18 +13,22 @@ namespace Volo.Abp.Account
private readonly IIdentityUserRepository _identityUserRepository; private readonly IIdentityUserRepository _identityUserRepository;
private readonly ILookupNormalizer _lookupNormalizer; private readonly ILookupNormalizer _lookupNormalizer;
private readonly IdentityUserManager _userManager; private readonly IdentityUserManager _userManager;
private readonly IOptions<IdentityOptions> _identityOptions;
public AccountAppService_Tests() public AccountAppService_Tests()
{ {
_accountAppService = GetRequiredService<IAccountAppService>(); _accountAppService = GetRequiredService<IAccountAppService>();
_identityUserRepository = GetRequiredService<IIdentityUserRepository>(); _identityUserRepository = GetRequiredService<IIdentityUserRepository>();
_lookupNormalizer = GetRequiredService<ILookupNormalizer>(); _lookupNormalizer = GetRequiredService<ILookupNormalizer>();
_userManager = GetRequiredService<IdentityUserManager>(); _userManager = GetRequiredService<IdentityUserManager>();
_identityOptions = GetRequiredService<IOptions<IdentityOptions>>();
} }
[Fact] [Fact]
public async Task RegisterAsync() public async Task RegisterAsync()
{ {
await _identityOptions.SetAsync();
var registerDto = new RegisterDto var registerDto = new RegisterDto
{ {
UserName = "bob.lee", UserName = "bob.lee",

8
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/ExternalLoginProviderBase.cs

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -14,23 +15,28 @@ namespace Volo.Abp.Identity
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
protected IdentityUserManager UserManager { get; } protected IdentityUserManager UserManager { get; }
protected IIdentityUserRepository IdentityUserRepository { get; } protected IIdentityUserRepository IdentityUserRepository { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; }
protected ExternalLoginProviderBase( protected ExternalLoginProviderBase(
IGuidGenerator guidGenerator, IGuidGenerator guidGenerator,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IdentityUserManager userManager, IdentityUserManager userManager,
IIdentityUserRepository identityUserRepository) IIdentityUserRepository identityUserRepository,
IOptions<IdentityOptions> identityOptions)
{ {
GuidGenerator = guidGenerator; GuidGenerator = guidGenerator;
CurrentTenant = currentTenant; CurrentTenant = currentTenant;
UserManager = userManager; UserManager = userManager;
IdentityUserRepository = identityUserRepository; IdentityUserRepository = identityUserRepository;
IdentityOptions = identityOptions;
} }
public abstract Task<bool> TryAuthenticateAsync(string userName, string plainPassword); public abstract Task<bool> TryAuthenticateAsync(string userName, string plainPassword);
public virtual async Task<IdentityUser> CreateUserAsync(string userName, string providerName) public virtual async Task<IdentityUser> CreateUserAsync(string userName, string providerName)
{ {
await IdentityOptions.SetAsync();
var externalUser = await GetUserInfoAsync(userName); var externalUser = await GetUserInfoAsync(userName);
NormalizeExternalLoginUserInfo(externalUser, userName); NormalizeExternalLoginUserInfo(externalUser, userName);

8
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDataSeeder.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -17,6 +18,7 @@ namespace Volo.Abp.Identity
protected IdentityUserManager UserManager { get; } protected IdentityUserManager UserManager { get; }
protected IdentityRoleManager RoleManager { get; } protected IdentityRoleManager RoleManager { get; }
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; }
public IdentityDataSeeder( public IdentityDataSeeder(
IGuidGenerator guidGenerator, IGuidGenerator guidGenerator,
@ -25,7 +27,8 @@ namespace Volo.Abp.Identity
ILookupNormalizer lookupNormalizer, ILookupNormalizer lookupNormalizer,
IdentityUserManager userManager, IdentityUserManager userManager,
IdentityRoleManager roleManager, IdentityRoleManager roleManager,
ICurrentTenant currentTenant) ICurrentTenant currentTenant,
IOptions<IdentityOptions> identityOptions)
{ {
GuidGenerator = guidGenerator; GuidGenerator = guidGenerator;
RoleRepository = roleRepository; RoleRepository = roleRepository;
@ -34,6 +37,7 @@ namespace Volo.Abp.Identity
UserManager = userManager; UserManager = userManager;
RoleManager = roleManager; RoleManager = roleManager;
CurrentTenant = currentTenant; CurrentTenant = currentTenant;
IdentityOptions = identityOptions;
} }
[UnitOfWork] [UnitOfWork]
@ -45,6 +49,8 @@ namespace Volo.Abp.Identity
Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail)); Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword)); Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));
await IdentityOptions.SetAsync();
var result = new IdentityDataSeedResult(); var result = new IdentityDataSeedResult();
using (CurrentTenant.Change(tenantId)) using (CurrentTenant.Change(tenantId))

8
modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/FakeExternalLoginProvider.cs

@ -1,5 +1,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -14,12 +16,14 @@ namespace Volo.Abp.Identity.AspNetCore
IGuidGenerator guidGenerator, IGuidGenerator guidGenerator,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IdentityUserManager userManager, IdentityUserManager userManager,
IIdentityUserRepository identityUserRepository) IIdentityUserRepository identityUserRepository,
IOptions<IdentityOptions> identityOptions)
: base( : base(
guidGenerator, guidGenerator,
currentTenant, currentTenant,
userManager, userManager,
identityUserRepository) identityUserRepository,
identityOptions)
{ {
} }

7
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Shouldly; using Shouldly;
using Volo.Abp.Uow; using Volo.Abp.Uow;
using Xunit; using Xunit;
@ -18,6 +19,7 @@ namespace Volo.Abp.Identity
private readonly ILookupNormalizer _lookupNormalizer; private readonly ILookupNormalizer _lookupNormalizer;
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IdentityTestData _testData; private readonly IdentityTestData _testData;
protected IOptions<IdentityOptions> _identityOptions { get; }
public IdentityUserManager_Tests() public IdentityUserManager_Tests()
{ {
@ -28,6 +30,7 @@ namespace Volo.Abp.Identity
_lookupNormalizer = GetRequiredService<ILookupNormalizer>(); _lookupNormalizer = GetRequiredService<ILookupNormalizer>();
_testData = GetRequiredService<IdentityTestData>(); _testData = GetRequiredService<IdentityTestData>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); _unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_identityOptions = GetRequiredService<IOptions<IdentityOptions>>();
} }
[Fact] [Fact]
@ -120,6 +123,8 @@ namespace Volo.Abp.Identity
[Fact] [Fact]
public async Task AddDefaultRolesAsync_In_Same_Uow() public async Task AddDefaultRolesAsync_In_Same_Uow()
{ {
await _identityOptions.SetAsync();
await CreateRandomDefaultRoleAsync(); await CreateRandomDefaultRoleAsync();
using (var uow = _unitOfWorkManager.Begin()) using (var uow = _unitOfWorkManager.Begin())
@ -176,6 +181,8 @@ namespace Volo.Abp.Identity
[Fact] [Fact]
public async Task AddDefaultRolesAsync_In_Different_Uow() public async Task AddDefaultRolesAsync_In_Different_Uow()
{ {
await _identityOptions.SetAsync();
await CreateRandomDefaultRoleAsync(); await CreateRandomDefaultRoleAsync();
Guid userId; Guid userId;

6
modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs

@ -33,6 +33,7 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
protected IStringLocalizer<AbpIdentityServerResource> Localizer { get; } protected IStringLocalizer<AbpIdentityServerResource> Localizer { get; }
protected IHybridServiceScopeFactory ServiceScopeFactory { get; } protected IHybridServiceScopeFactory ServiceScopeFactory { get; }
protected AbpIdentityOptions AbpIdentityOptions { get; } protected AbpIdentityOptions AbpIdentityOptions { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; }
public AbpResourceOwnerPasswordValidator( public AbpResourceOwnerPasswordValidator(
UserManager<IdentityUser> userManager, UserManager<IdentityUser> userManager,
@ -41,7 +42,8 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
ILogger<ResourceOwnerPasswordValidator<IdentityUser>> logger, ILogger<ResourceOwnerPasswordValidator<IdentityUser>> logger,
IStringLocalizer<AbpIdentityServerResource> localizer, IStringLocalizer<AbpIdentityServerResource> localizer,
IOptions<AbpIdentityOptions> abpIdentityOptions, IOptions<AbpIdentityOptions> abpIdentityOptions,
IHybridServiceScopeFactory serviceScopeFactory) IHybridServiceScopeFactory serviceScopeFactory,
IOptions<IdentityOptions> identityOptions)
{ {
UserManager = userManager; UserManager = userManager;
SignInManager = signInManager; SignInManager = signInManager;
@ -50,6 +52,7 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
Localizer = localizer; Localizer = localizer;
ServiceScopeFactory = serviceScopeFactory; ServiceScopeFactory = serviceScopeFactory;
AbpIdentityOptions = abpIdentityOptions.Value; AbpIdentityOptions = abpIdentityOptions.Value;
IdentityOptions = identityOptions;
} }
/// <summary> /// <summary>
@ -123,6 +126,7 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
string errorDescription; string errorDescription;
if (user != null) if (user != null)
{ {
await IdentityOptions.SetAsync();
var result = await SignInManager.CheckPasswordSignInAsync(user, context.Password, true); var result = await SignInManager.CheckPasswordSignInAsync(user, context.Password, true);
if (result.Succeeded) if (result.Succeeded)
{ {

Loading…
Cancel
Save