diff --git a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs b/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs index db519d7c..8d6a5539 100644 --- a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs +++ b/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs @@ -41,14 +41,25 @@ public class EShopUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory public override async Task CreateAsync(IdentityUser user) { var principal = await base.CreateAsync(user); - if (HttpContext.Request.Cookies.ContainsKey(EShopConstants.AnonymousUserCookieName)) - { - HttpContext.Request.Cookies.TryGetValue(EShopConstants.AnonymousUserCookieName, out var anonymousUserId); + // if (HttpContext.Request.Cookies.ContainsKey(EShopConstants.AnonymousUserClaimName)) + // { + HttpContext.Request.Cookies.TryGetValue(EShopConstants.AnonymousUserClaimName, out var anonymousUserId); principal.Identities.First() .AddClaim(new Claim(EShopConstants.AnonymousUserClaimName, anonymousUserId)); _logger.LogInformation( $"Added {EShopConstants.AnonymousUserClaimName} claim of AnonymousUserId from cookies:{anonymousUserId}"); + // } + + // You can notice that cookie contains anonymous_id + foreach (var cookie in HttpContext.Request.Cookies.ToList()) + { + _logger.LogInformation($"key:{cookie.Key} - value:{cookie.Value}"); + } + _logger.LogInformation($"===================="); + foreach (var claim in principal.Claims.ToList()) + { + _logger.LogInformation($"key:{claim.Type} - value:{claim.Value}"); } return principal; diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs index 2fd7e5de..df7fc7d5 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs @@ -1,12 +1,11 @@ -using Castle.Core.Logging; -using EShopOnAbp.BasketService; +using EShopOnAbp.BasketService; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using EShopOnAbp.Shared.Hosting.AspNetCore; +using Microsoft.AspNetCore.Authentication; using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; using Volo.Abp.Users; @@ -41,8 +40,10 @@ namespace EShopOnAbp.PublicWeb.Basket { try { + var access_token = await HttpContext.GetTokenAsync("access_token"); + logger.LogInformation($"ACCESS_TOKEN:{access_token}"); // Get anonymous user id from cookie - HttpContext.Request.Cookies.TryGetValue(CookieConstants.AnonymousUserCookieName, + HttpContext.Request.Cookies.TryGetValue(EShopConstants.AnonymousUserClaimName, out string anonymousUserId); logger.LogInformation($"========= Anonymous User Id from Cookie:{anonymousUserId} ========= "); @@ -50,8 +51,13 @@ namespace EShopOnAbp.PublicWeb.Basket if (string.IsNullOrEmpty(anonymousUserId)) { anonymousUserId = guidGenerator.Create().ToString(); - HttpContext.Response.Cookies.Append(CookieConstants.AnonymousUserCookieName, anonymousUserId); - logger.LogInformation($"========= Generated new User Id:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); + HttpContext.Response.Cookies.Append(EShopConstants.AnonymousUserClaimName, anonymousUserId, + new CookieOptions + { + SameSite = SameSiteMode.Lax + }); + logger.LogInformation( + $"========= Generated new User Id:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); } if (!currentUser.IsAuthenticated) @@ -60,11 +66,20 @@ namespace EShopOnAbp.PublicWeb.Basket $"========= Get Basket for Anonymous UserId:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); return await basketAppService.GetByAnonymousUserIdAsync(Guid.Parse(anonymousUserId)); } - else + + //TODO: Merge with anonymously stored cart if exist + var userClaimValue = currentUser.FindClaimValue(EShopConstants.AnonymousUserClaimName); + foreach (var claim in currentUser.GetAllClaims()) + { + logger.LogInformation($"Claim Type:{claim.Type}-Value:{claim.Value}"); + } + + if (string.IsNullOrEmpty(userClaimValue)) { - //TODO: Merge with anonymously stored cart if exist return await basketAppService.GetAsync(); } + + return await basketAppService.MergeBasketsAsync(); } catch (Exception ex) { @@ -73,4 +88,4 @@ namespace EShopOnAbp.PublicWeb.Basket } } } -} +} \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs index d912d267..a3a83f9c 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs @@ -110,6 +110,8 @@ namespace EShopOnAbp.PublicWeb options.Scope.Add("AdministrationService"); options.Scope.Add("BasketService"); options.Scope.Add("CatalogService"); + + options.ClaimActions.MapUniqueJsonKey(EShopConstants.AnonymousUserClaimName, EShopConstants.AnonymousUserClaimName); }); var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);