Browse Source

added logging for diagnose

pull/45/head
Galip Tolga Erdem 5 years ago
parent
commit
ed47cb47f7
  1. 17
      apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs
  2. 35
      apps/public-web/src/EShopOnAbp.PublicWeb/Basket/UserBasketProvider.cs
  3. 2
      apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs

17
apps/auth-server/src/EShopOnAbp.AuthServer/EShopUserClaimsPrincipalFactory.cs

@ -41,14 +41,25 @@ public class EShopUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory
public override async Task<ClaimsPrincipal> 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;

35
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
}
}
}
}
}

2
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"]);

Loading…
Cancel
Save