Browse Source

moved anonymous_id creation to viewComponent

pull/47/head
Galip Tolga Erdem 5 years ago
parent
commit
192a7abb5c
  1. 15
      apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs
  2. 13
      apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs
  3. 48
      apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/AnonymousUserViewComponent.cs
  4. 3
      apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/Default.cshtml
  5. 4
      apps/public-web/src/EShopOnAbp.PublicWeb/Pages/_ViewStart.cshtml

15
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));

13
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)
{

48
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<AnonymousUserViewComponent> _logger;
private readonly IGuidGenerator _guidGenerator;
public AnonymousUserViewComponent(ILogger<AnonymousUserViewComponent> logger, IGuidGenerator guidGenerator)
{
_logger = logger;
_guidGenerator = guidGenerator;
}
public async Task<IViewComponentResult> 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"));
}
}

3
apps/public-web/src/EShopOnAbp.PublicWeb/Components/AnonymousUser/Default.cshtml

@ -0,0 +1,3 @@
<div>
</div>

4
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))
Loading…
Cancel
Save