diff --git a/EssentialCSharp.Web/Controllers/BaseController.cs b/EssentialCSharp.Web/Controllers/BaseController.cs index f1bdbb89..94f0421c 100644 --- a/EssentialCSharp.Web/Controllers/BaseController.cs +++ b/EssentialCSharp.Web/Controllers/BaseController.cs @@ -1,3 +1,4 @@ +using EssentialCSharp.Web.Extensions; using EssentialCSharp.Web.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; @@ -7,16 +8,22 @@ namespace EssentialCSharp.Web.Controllers; public abstract class BaseController : Controller { private readonly IRouteConfigurationService _RouteConfigurationService; + private readonly IHttpContextAccessor _HttpContextAccessor; - protected BaseController(IRouteConfigurationService routeConfigurationService) + protected BaseController(IRouteConfigurationService routeConfigurationService, IHttpContextAccessor httpContextAccessor) { _RouteConfigurationService = routeConfigurationService; + _HttpContextAccessor = httpContextAccessor; } public override void OnActionExecuting(ActionExecutingContext context) { // Automatically add static routes to all views ViewBag.StaticRoutes = System.Text.Json.JsonSerializer.Serialize(_RouteConfigurationService.GetStaticRoutes()); + + // Set the referral Id for use in the front end if available + ViewBag.ReferralId = _HttpContextAccessor.HttpContext.GetReferrerId(); + base.OnActionExecuting(context); } } diff --git a/EssentialCSharp.Web/Controllers/HomeController.cs b/EssentialCSharp.Web/Controllers/HomeController.cs index 3495a7d6..2bc1c463 100644 --- a/EssentialCSharp.Web/Controllers/HomeController.cs +++ b/EssentialCSharp.Web/Controllers/HomeController.cs @@ -7,7 +7,7 @@ namespace EssentialCSharp.Web.Controllers; -public class HomeController(ILogger logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, IHttpContextAccessor httpContextAccessor) : Controller +public class HomeController(ILogger logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, IHttpContextAccessor httpContextAccessor, IRouteConfigurationService routeConfigurationService) : BaseController(routeConfigurationService, httpContextAccessor) { public IActionResult Index() { @@ -34,8 +34,6 @@ public IActionResult Index() ViewBag.PreviousPage = FlipPage(siteMapping.ChapterNumber, siteMapping.PageNumber, false); ViewBag.HeadContents = headHtml; ViewBag.Contents = html; - // Set the referral Id for use in the front end if available - ViewBag.ReferralId = httpContextAccessor.HttpContext?.User?.Claims?.FirstOrDefault(f => f.Type == ClaimsExtensions.ReferrerIdClaimType)?.Value; return View(); } else diff --git a/EssentialCSharp.Web/Extensions/ClaimsExtensions.cs b/EssentialCSharp.Web/Extensions/ClaimsExtensions.cs index 3705cb83..b969f5cf 100644 --- a/EssentialCSharp.Web/Extensions/ClaimsExtensions.cs +++ b/EssentialCSharp.Web/Extensions/ClaimsExtensions.cs @@ -13,5 +13,16 @@ public static class ClaimsExtensions { return claims.FirstOrDefault(claim => claim.Type == ReferrerIdClaimType)?.Value; } + + /// + /// Gets the referral ID from the current user's claims in the HttpContext + /// + /// The HttpContext to get the referral ID from + /// The referral ID if found, otherwise null + public static string? GetReferrerId(this HttpContext? httpContext) + { + return httpContext?.User?.GetReferrerId(); + } + public const string ReferrerIdClaimType = "ReferrerId"; } diff --git a/EssentialCSharp.Web/Services/Referrals/ReferralService.cs b/EssentialCSharp.Web/Services/Referrals/ReferralService.cs index fea2cfb5..4d0e1325 100644 --- a/EssentialCSharp.Web/Services/Referrals/ReferralService.cs +++ b/EssentialCSharp.Web/Services/Referrals/ReferralService.cs @@ -62,7 +62,7 @@ public class ReferralService(EssentialCSharpWebContext dbContext, UserManager c.Type == ClaimsExtensions.ReferrerIdClaimType)?.Value; + string? claimsReferrerId = user?.GetReferrerId(); if (claimsReferrerId == referralId) {