Clear the dynamic cache when user logs in.

pull/18064/head
maliming 2 years ago
parent 3b09f26e68
commit 0a3be97a28
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

@ -10,7 +10,6 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Security.Principal;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Volo.Abp.Account.Settings; using Volo.Abp.Account.Settings;
@ -32,14 +31,12 @@ public class IdentityServerSupportedLoginModel : LoginModel
public IdentityServerSupportedLoginModel( public IdentityServerSupportedLoginModel(
IAuthenticationSchemeProvider schemeProvider, IAuthenticationSchemeProvider schemeProvider,
IOptions<AbpAccountOptions> accountOptions, IOptions<AbpAccountOptions> accountOptions,
IOptions<IdentityOptions> identityOptions,
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache,
IIdentityServerInteractionService interaction, IIdentityServerInteractionService interaction,
IClientStore clientStore, IClientStore clientStore,
IEventService identityServerEvents, IEventService identityServerEvents)
IOptions<IdentityOptions> identityOptions) : base(schemeProvider, accountOptions, identityOptions, identityDynamicClaimsPrincipalContributorCache)
: base(
schemeProvider,
accountOptions,
identityOptions)
{ {
Interaction = interaction; Interaction = interaction;
ClientStore = clientStore; ClientStore = clientStore;
@ -177,6 +174,9 @@ public class IdentityServerSupportedLoginModel : LoginModel
Debug.Assert(user != null, nameof(user) + " != null"); Debug.Assert(user != null, nameof(user) + " != null");
await IdentityServerEvents.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id.ToString(), user.UserName)); //TODO: Use user's name once implemented await IdentityServerEvents.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id.ToString(), user.UserName)); //TODO: Use user's name once implemented
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
return RedirectSafely(ReturnUrl, ReturnUrlHash); return RedirectSafely(ReturnUrl, ReturnUrlHash);
} }

@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc;
using OpenIddict.Server; using OpenIddict.Server;
using OpenIddict.Server.AspNetCore; using OpenIddict.Server.AspNetCore;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.OpenIddict; using Volo.Abp.OpenIddict;
@ -17,12 +18,14 @@ namespace Volo.Abp.Account.Web.Pages.Account;
public class OpenIddictSupportedLoginModel : LoginModel public class OpenIddictSupportedLoginModel : LoginModel
{ {
protected AbpOpenIddictRequestHelper OpenIddictRequestHelper { get; } protected AbpOpenIddictRequestHelper OpenIddictRequestHelper { get; }
public OpenIddictSupportedLoginModel( public OpenIddictSupportedLoginModel(
IAuthenticationSchemeProvider schemeProvider, IAuthenticationSchemeProvider schemeProvider,
IOptions<AbpAccountOptions> accountOptions, IOptions<AbpAccountOptions> accountOptions,
IOptions<IdentityOptions> identityOptions, IOptions<IdentityOptions> identityOptions,
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache,
AbpOpenIddictRequestHelper openIddictRequestHelper) AbpOpenIddictRequestHelper openIddictRequestHelper)
: base(schemeProvider, accountOptions, identityOptions) : base(schemeProvider, accountOptions, identityOptions, identityDynamicClaimsPrincipalContributorCache)
{ {
OpenIddictRequestHelper = openIddictRequestHelper; OpenIddictRequestHelper = openIddictRequestHelper;
} }

@ -29,13 +29,15 @@ public class AccountController : AbpControllerBase
protected ISettingProvider SettingProvider { get; } protected ISettingProvider SettingProvider { get; }
protected IdentitySecurityLogManager IdentitySecurityLogManager { get; } protected IdentitySecurityLogManager IdentitySecurityLogManager { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; } protected IOptions<IdentityOptions> IdentityOptions { get; }
protected IdentityDynamicClaimsPrincipalContributorCache IdentityDynamicClaimsPrincipalContributorCache { 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) IOptions<IdentityOptions> identityOptions,
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache)
{ {
LocalizationResource = typeof(AccountResource); LocalizationResource = typeof(AccountResource);
@ -44,6 +46,7 @@ public class AccountController : AbpControllerBase
SettingProvider = settingProvider; SettingProvider = settingProvider;
IdentitySecurityLogManager = identitySecurityLogManager; IdentitySecurityLogManager = identitySecurityLogManager;
IdentityOptions = identityOptions; IdentityOptions = identityOptions;
IdentityDynamicClaimsPrincipalContributorCache = identityDynamicClaimsPrincipalContributorCache;
} }
[HttpPost] [HttpPost]
@ -69,6 +72,16 @@ public class AccountController : AbpControllerBase
UserName = login.UserNameOrEmailAddress UserName = login.UserNameOrEmailAddress
}); });
if (signInResult.Succeeded)
{
var user = await UserManager.FindByNameAsync(login.UserNameOrEmailAddress);
if (user != null)
{
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
}
}
return GetAbpLoginResult(signInResult); return GetAbpLoginResult(signInResult);
} }

@ -52,17 +52,19 @@ public class LoginModel : AccountPageModel
protected IAuthenticationSchemeProvider SchemeProvider { get; } protected IAuthenticationSchemeProvider SchemeProvider { get; }
protected AbpAccountOptions AccountOptions { get; } protected AbpAccountOptions AccountOptions { get; }
protected IOptions<IdentityOptions> IdentityOptions { get; } protected IOptions<IdentityOptions> IdentityOptions { get; }
protected IdentityDynamicClaimsPrincipalContributorCache IdentityDynamicClaimsPrincipalContributorCache { get; }
public bool ShowCancelButton { get; set; } public bool ShowCancelButton { get; set; }
public LoginModel( public LoginModel(
IAuthenticationSchemeProvider schemeProvider, IAuthenticationSchemeProvider schemeProvider,
IOptions<AbpAccountOptions> accountOptions, IOptions<AbpAccountOptions> accountOptions,
IOptions<IdentityOptions> identityOptions) IOptions<IdentityOptions> identityOptions,
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache)
{ {
SchemeProvider = schemeProvider; SchemeProvider = schemeProvider;
IdentityOptions = identityOptions; IdentityOptions = identityOptions;
AccountOptions = accountOptions.Value; AccountOptions = accountOptions.Value;
IdentityDynamicClaimsPrincipalContributorCache = identityDynamicClaimsPrincipalContributorCache;
} }
public virtual async Task<IActionResult> OnGetAsync() public virtual async Task<IActionResult> OnGetAsync()
@ -138,6 +140,9 @@ public class LoginModel : AccountPageModel
Debug.Assert(user != null, nameof(user) + " != null"); Debug.Assert(user != null, nameof(user) + " != null");
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
return RedirectSafely(ReturnUrl, ReturnUrlHash); return RedirectSafely(ReturnUrl, ReturnUrlHash);
} }
@ -222,8 +227,16 @@ public class LoginModel : AccountPageModel
throw new UserFriendlyException("Cannot proceed because user is not allowed!"); throw new UserFriendlyException("Cannot proceed because user is not allowed!");
} }
IdentityUser user;
if (result.Succeeded) if (result.Succeeded)
{ {
user = await UserManager.FindByLoginAsync(loginInfo.LoginProvider, loginInfo.ProviderKey);
if (user != null)
{
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
}
return RedirectSafely(returnUrl, returnUrlHash); return RedirectSafely(returnUrl, returnUrlHash);
} }
@ -239,7 +252,7 @@ public class LoginModel : AccountPageModel
}); });
} }
var user = await UserManager.FindByEmailAsync(email); user = await UserManager.FindByEmailAsync(email);
if (user == null) if (user == null)
{ {
return RedirectToPage("./Register", new { return RedirectToPage("./Register", new {
@ -263,6 +276,9 @@ public class LoginModel : AccountPageModel
UserName = user.Name UserName = user.Name
}); });
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
return RedirectSafely(returnUrl, returnUrlHash); return RedirectSafely(returnUrl, returnUrlHash);
} }

@ -46,13 +46,16 @@ public class RegisterModel : AccountPageModel
protected IAuthenticationSchemeProvider SchemeProvider { get; } protected IAuthenticationSchemeProvider SchemeProvider { get; }
protected AbpAccountOptions AccountOptions { get; } protected AbpAccountOptions AccountOptions { get; }
protected IdentityDynamicClaimsPrincipalContributorCache IdentityDynamicClaimsPrincipalContributorCache { get; }
public RegisterModel( public RegisterModel(
IAccountAppService accountAppService, IAccountAppService accountAppService,
IAuthenticationSchemeProvider schemeProvider, IAuthenticationSchemeProvider schemeProvider,
IOptions<AbpAccountOptions> accountOptions) IOptions<AbpAccountOptions> accountOptions,
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache)
{ {
SchemeProvider = schemeProvider; SchemeProvider = schemeProvider;
IdentityDynamicClaimsPrincipalContributorCache = identityDynamicClaimsPrincipalContributorCache;
AccountAppService = accountAppService; AccountAppService = accountAppService;
AccountOptions = accountOptions.Value; AccountOptions = accountOptions.Value;
} }
@ -159,6 +162,9 @@ public class RegisterModel : AccountPageModel
var user = await UserManager.GetByIdAsync(userDto.Id); var user = await UserManager.GetByIdAsync(userDto.Id);
await SignInManager.SignInAsync(user, isPersistent: true); await SignInManager.SignInAsync(user, isPersistent: true);
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
} }
protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string userName, string emailAddress) protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string userName, string emailAddress)
@ -185,6 +191,9 @@ public class RegisterModel : AccountPageModel
} }
await SignInManager.SignInAsync(user, isPersistent: true, ExternalLoginAuthSchema); await SignInManager.SignInAsync(user, isPersistent: true, ExternalLoginAuthSchema);
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
} }
protected virtual async Task<bool> CheckSelfRegistrationAsync() protected virtual async Task<bool> CheckSelfRegistrationAsync()

@ -31,6 +31,7 @@ public partial class TokenController
protected IdentitySecurityLogManager IdentitySecurityLogManager => LazyServiceProvider.LazyGetRequiredService<IdentitySecurityLogManager>(); protected IdentitySecurityLogManager IdentitySecurityLogManager => LazyServiceProvider.LazyGetRequiredService<IdentitySecurityLogManager>();
protected ISettingProvider SettingProvider => LazyServiceProvider.LazyGetRequiredService<ISettingProvider>(); protected ISettingProvider SettingProvider => LazyServiceProvider.LazyGetRequiredService<ISettingProvider>();
protected IdentityDynamicClaimsPrincipalContributorCache IdentityDynamicClaimsPrincipalContributorCache => LazyServiceProvider.LazyGetRequiredService<IdentityDynamicClaimsPrincipalContributorCache>();
[UnitOfWork] [UnitOfWork]
protected virtual async Task<IActionResult> HandlePasswordAsync(OpenIddictRequest request) protected virtual async Task<IActionResult> HandlePasswordAsync(OpenIddictRequest request)
@ -334,6 +335,9 @@ public partial class TokenController
protected virtual async Task<IActionResult> SetSuccessResultAsync(OpenIddictRequest request, IdentityUser user) protected virtual async Task<IActionResult> SetSuccessResultAsync(OpenIddictRequest request, IdentityUser user)
{ {
// Clear the dynamic claims cache.
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(user.Id, user.TenantId);
// Create a new ClaimsPrincipal containing the claims that // Create a new ClaimsPrincipal containing the claims that
// will be used to create an id_token, a token or a code. // will be used to create an id_token, a token or a code.
var principal = await SignInManager.CreateUserPrincipalAsync(user); var principal = await SignInManager.CreateUserPrincipalAsync(user);

Loading…
Cancel
Save