diff --git a/apps/auth-server/src/EShopOnAbp.AuthServer/AnonymousUserItem.cs b/apps/auth-server/src/EShopOnAbp.AuthServer/AnonymousUserItem.cs deleted file mode 100644 index 3679543d..00000000 --- a/apps/auth-server/src/EShopOnAbp.AuthServer/AnonymousUserItem.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace EShopOnAbp.AuthServer; - -public class AnonymousUserItem -{ - public string AnonymousUserId { get; set; } -} \ No newline at end of file diff --git a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs b/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs deleted file mode 100644 index d4368878..00000000 --- a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs +++ /dev/null @@ -1,82 +0,0 @@ -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 _logger; - private readonly IDistributedCache _cache; - protected HttpContext HttpContext => _httpContextAccessor.HttpContext; - - public EShopUserClaimsPrincipalFactory( - UserManager userManager, - RoleManager roleManager, - IOptions options, - ICurrentPrincipalAccessor currentPrincipalAccessor, - IAbpClaimsPrincipalFactory abpClaimsPrincipalFactory, - IHttpContextAccessor httpContextAccessor, - ILogger logger, - IDistributedCache cache) : base(userManager, - roleManager, - options, - currentPrincipalAccessor, - abpClaimsPrincipalFactory) - { - _httpContextAccessor = httpContextAccessor; - _logger = logger; - _cache = cache; - } - - public override async Task 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); - if (cachedItem == null) - { - _logger.LogWarning($"Could not retrieve anonymousUserId from cache."); - return principal; - } - - _logger.LogInformation($"Retrieved anonymousUserId:{cachedItem.AnonymousUserId} from cache."); - - principal.Identities.First().AddClaim( - new Claim( - EShopConstants.AnonymousUserClaimName, - cachedItem.AnonymousUserId - ) - ); - - return principal; - } -} \ No newline at end of file diff --git a/shared/EShopOnAbp.Shared.Hosting.AspNetCore/EShopConstants.cs b/shared/EShopOnAbp.Shared.Hosting.AspNetCore/EShopConstants.cs index 2e023cea..6f7d0db7 100644 --- a/shared/EShopOnAbp.Shared.Hosting.AspNetCore/EShopConstants.cs +++ b/shared/EShopOnAbp.Shared.Hosting.AspNetCore/EShopConstants.cs @@ -2,6 +2,5 @@ public static class EShopConstants { - // public const string AnonymousUserCookieName = "eshop_anonymousId"; public const string AnonymousUserClaimName = "anonymous_id"; } \ No newline at end of file