Skip to content

Commit 999eff0

Browse files
Add Referral Id to all pages
1 parent fab8f9b commit 999eff0

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

EssentialCSharp.Web/Controllers/BaseController.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using EssentialCSharp.Web.Extensions;
12
using EssentialCSharp.Web.Services;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.AspNetCore.Mvc.Filters;
@@ -7,16 +8,22 @@ namespace EssentialCSharp.Web.Controllers;
78
public abstract class BaseController : Controller
89
{
910
private readonly IRouteConfigurationService _RouteConfigurationService;
11+
private readonly IHttpContextAccessor _HttpContextAccessor;
1012

11-
protected BaseController(IRouteConfigurationService routeConfigurationService)
13+
protected BaseController(IRouteConfigurationService routeConfigurationService, IHttpContextAccessor httpContextAccessor)
1214
{
1315
_RouteConfigurationService = routeConfigurationService;
16+
_HttpContextAccessor = httpContextAccessor;
1417
}
1518

1619
public override void OnActionExecuting(ActionExecutingContext context)
1720
{
1821
// Automatically add static routes to all views
1922
ViewBag.StaticRoutes = System.Text.Json.JsonSerializer.Serialize(_RouteConfigurationService.GetStaticRoutes());
23+
24+
// Set the referral Id for use in the front end if available
25+
ViewBag.ReferralId = _HttpContextAccessor.HttpContext.GetReferrerId();
26+
2027
base.OnActionExecuting(context);
2128
}
2229
}

EssentialCSharp.Web/Controllers/HomeController.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace EssentialCSharp.Web.Controllers;
99

10-
public class HomeController(ILogger<HomeController> logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, IHttpContextAccessor httpContextAccessor) : Controller
10+
public class HomeController(ILogger<HomeController> logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, IHttpContextAccessor httpContextAccessor, IRouteConfigurationService routeConfigurationService) : BaseController(routeConfigurationService, httpContextAccessor)
1111
{
1212
public IActionResult Index()
1313
{
@@ -34,8 +34,6 @@ public IActionResult Index()
3434
ViewBag.PreviousPage = FlipPage(siteMapping.ChapterNumber, siteMapping.PageNumber, false);
3535
ViewBag.HeadContents = headHtml;
3636
ViewBag.Contents = html;
37-
// Set the referral Id for use in the front end if available
38-
ViewBag.ReferralId = httpContextAccessor.HttpContext?.User?.Claims?.FirstOrDefault(f => f.Type == ClaimsExtensions.ReferrerIdClaimType)?.Value;
3937
return View();
4038
}
4139
else

EssentialCSharp.Web/Extensions/ClaimsExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,16 @@ public static class ClaimsExtensions
1313
{
1414
return claims.FirstOrDefault(claim => claim.Type == ReferrerIdClaimType)?.Value;
1515
}
16+
17+
/// <summary>
18+
/// Gets the referral ID from the current user's claims in the HttpContext
19+
/// </summary>
20+
/// <param name="httpContext">The HttpContext to get the referral ID from</param>
21+
/// <returns>The referral ID if found, otherwise null</returns>
22+
public static string? GetReferrerId(this HttpContext? httpContext)
23+
{
24+
return httpContext?.User?.GetReferrerId();
25+
}
26+
1627
public const string ReferrerIdClaimType = "ReferrerId";
1728
}

EssentialCSharp.Web/Services/Referrals/ReferralService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class ReferralService(EssentialCSharpWebContext dbContext, UserManager<Es
6262
public void TrackReferralAsync(string referralId, ClaimsPrincipal? user)
6363
{
6464
// Check if the referrer ID exists in the claims principal
65-
string? claimsReferrerId = user?.Claims.FirstOrDefault(c => c.Type == ClaimsExtensions.ReferrerIdClaimType)?.Value;
65+
string? claimsReferrerId = user?.GetReferrerId();
6666

6767
if (claimsReferrerId == referralId)
6868
{

0 commit comments

Comments
 (0)