From 192a7abb5c9c6d2387923f58024030df78cc6bf2 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Fri, 24 Dec 2021 15:57:22 +0300 Subject: [PATCH] moved anonymous_id creation to viewComponent --- .../EShopUserClaimsPrincipalFactory.cs | 15 ++++-- .../Basket/UserBasketProvider.cs | 13 ----- .../AnonymousUserViewComponent.cs | 48 +++++++++++++++++++ .../Components/AnonymousUser/Default.cshtml | 3 ++ .../Pages/_ViewStart.cshtml | 4 +- 5 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/AnonymousUserViewComponent.cs create mode 100644 apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/Default.cshtml diff --git a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs b/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs index 32d0e1fc..24421034 100644 --- a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs +++ b/apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs @@ -52,14 +52,21 @@ public class EShopUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory { 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) - }); + 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)); diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs index 6abfc1b4..592908b7 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs @@ -41,19 +41,6 @@ namespace EShopOnAbp.PublicWeb.Basket // Get anonymous user id from cookie HttpContext.Request.Cookies.TryGetValue(EShopConstants.AnonymousUserClaimName, out string anonymousUserId); - logger.LogInformation($"========= Anonymous User Id from Cookie:{anonymousUserId} ========= "); - - // Generate new id for anonymous user - if (string.IsNullOrEmpty(anonymousUserId)) - { - anonymousUserId = guidGenerator.Create().ToString(); - 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) { diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/AnonymousUserViewComponent.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/AnonymousUserViewComponent.cs new file mode 100644 index 00000000..570c6c01 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/AnonymousUserViewComponent.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using EShopOnAbp.Shared.Hosting.AspNetCore; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.Widgets; +using Volo.Abp.Guids; + +namespace EShopOnAbp.PublicWeb.Components.AnonymousUser; + +[Widget( + AutoInitialize = true +)] +public class AnonymousUserViewComponent : AbpViewComponent +{ + private readonly ILogger _logger; + private readonly IGuidGenerator _guidGenerator; + + public AnonymousUserViewComponent(ILogger logger, IGuidGenerator guidGenerator) + { + _logger = logger; + _guidGenerator = guidGenerator; + } + + public async Task InvokeAsync() + { + // Get anonymous user id from cookie + HttpContext.Request.Cookies.TryGetValue(EShopConstants.AnonymousUserClaimName, + out string anonymousUserId); + _logger.LogInformation($"========= Anonymous User Id from Cookie:{anonymousUserId} ========= "); + + // Generate new id for anonymous user + if (string.IsNullOrEmpty(anonymousUserId)) + { + anonymousUserId = _guidGenerator.Create().ToString(); + HttpContext.Response.Cookies.Append(EShopConstants.AnonymousUserClaimName, anonymousUserId, + new CookieOptions + { + SameSite = SameSiteMode.Lax + }); + _logger.LogInformation( + $"========= Generated new User Id:{anonymousUserId} ========= APPENDED TO COOKIE ====== "); + } + return await Task.FromResult(View("~/Components/AnonymousUser/Default.cshtml")); + } +} \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/Default.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/Default.cshtml new file mode 100644 index 00000000..42eb8c53 --- /dev/null +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/Default.cshtml @@ -0,0 +1,3 @@ +
+ +
\ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/_ViewStart.cshtml b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/_ViewStart.cshtml index 932987df..2f921a29 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/_ViewStart.cshtml +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/Pages/_ViewStart.cshtml @@ -1,5 +1,7 @@ -@using Volo.Abp.AspNetCore.Mvc.UI.Theming +@using EShopOnAbp.PublicWeb.Components.AnonymousUser +@using Volo.Abp.AspNetCore.Mvc.UI.Theming @inject IThemeManager ThemeManager @{ Layout = ThemeManager.CurrentTheme.GetPublicLayout(); } +@await Component.InvokeAsync(typeof(AnonymousUserViewComponent)) \ No newline at end of file