committed by
GitHub
15 changed files with 224 additions and 52 deletions
@ -0,0 +1,6 @@ |
|||
namespace EShopOnAbp.AuthServer; |
|||
|
|||
public class AnonymousUserItem |
|||
{ |
|||
public string AnonymousUserId { get; set; } |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
using EShopOnAbp.Shared.Hosting.AspNetCore; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Microsoft.Extensions.Caching.Distributed; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Identity; |
|||
using Volo.Abp.Security.Claims; |
|||
using IdentityRole = Volo.Abp.Identity.IdentityRole; |
|||
using IdentityUser = Volo.Abp.Identity.IdentityUser; |
|||
|
|||
namespace EShopOnAbp.AuthServer; |
|||
|
|||
[Dependency(ReplaceServices = true)] |
|||
[ExposeServices(typeof(AbpUserClaimsPrincipalFactory))] |
|||
public class EShopUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory |
|||
{ |
|||
private readonly IHttpContextAccessor _httpContextAccessor; |
|||
private readonly ILogger<EShopUserClaimsPrincipalFactory> _logger; |
|||
private readonly IDistributedCache<AnonymousUserItem, Guid> _cache; |
|||
protected HttpContext HttpContext => _httpContextAccessor.HttpContext; |
|||
|
|||
public EShopUserClaimsPrincipalFactory( |
|||
UserManager<IdentityUser> userManager, |
|||
RoleManager<IdentityRole> roleManager, |
|||
IOptions<IdentityOptions> options, |
|||
ICurrentPrincipalAccessor currentPrincipalAccessor, |
|||
IAbpClaimsPrincipalFactory abpClaimsPrincipalFactory, |
|||
IHttpContextAccessor httpContextAccessor, |
|||
ILogger<EShopUserClaimsPrincipalFactory> logger, |
|||
IDistributedCache<AnonymousUserItem, Guid> cache) : base(userManager, |
|||
roleManager, |
|||
options, |
|||
currentPrincipalAccessor, |
|||
abpClaimsPrincipalFactory) |
|||
{ |
|||
_httpContextAccessor = httpContextAccessor; |
|||
_logger = logger; |
|||
_cache = cache; |
|||
} |
|||
|
|||
public override async Task<ClaimsPrincipal> CreateAsync(IdentityUser user) |
|||
{ |
|||
var principal = await base.CreateAsync(user); |
|||
if (HttpContext.Request.Cookies.ContainsKey(EShopConstants.AnonymousUserClaimName)) |
|||
{ |
|||
HttpContext.Request.Cookies.TryGetValue(EShopConstants.AnonymousUserClaimName, out var anonymousUserId); |
|||
|
|||
await _cache.SetAsync(user.Id, new AnonymousUserItem {AnonymousUserId = anonymousUserId}, options: new DistributedCacheEntryOptions() |
|||
{ |
|||
AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(60) |
|||
}); |
|||
_logger.LogInformation($"Cached anonymousUserId:{anonymousUserId}."); |
|||
} |
|||
|
|||
var cachedItem = await _cache.GetAsync(user.Id); |
|||
_logger.LogInformation($"Retrieved anonymousUserId:{cachedItem.AnonymousUserId} from cache."); |
|||
principal.Identities.First() |
|||
.AddClaim(new Claim(EShopConstants.AnonymousUserClaimName, cachedItem.AnonymousUserId)); |
|||
|
|||
return principal; |
|||
} |
|||
} |
|||
@ -1,8 +0,0 @@ |
|||
using System; |
|||
|
|||
namespace EShopOnAbp.PublicWeb; |
|||
|
|||
public static class CookieConstants |
|||
{ |
|||
public const string AnonymousUserCookieName = "eshop_anonymousId"; |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace EShopOnAbp.Shared.Hosting.AspNetCore; |
|||
|
|||
public static class EShopConstants |
|||
{ |
|||
// public const string AnonymousUserCookieName = "eshop_anonymousId";
|
|||
public const string AnonymousUserClaimName = "anonymous_id"; |
|||
} |
|||
Loading…
Reference in new issue