Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Minor changes - Use method GetTypedHeaders for get referer url

KrzysztofPajak143f2b16c21d
diff --git a/src/Web/Grand.Web.Common/Controllers/BaseController.cs b/src/Web/Grand.Web.Common/Controllers/BaseController.cs
index 48e5d07dd..4b9157f72 100644
--- a/src/Web/Grand.Web.Common/Controllers/BaseController.cs
+++ b/src/Web/Grand.Web.Common/Controllers/BaseController.cs
@@ -8,12 +8,12 @@ using Grand.Web.Common.Filters;
using Grand.Web.Common.Models;
using Grand.Web.Common.Page;
using MediatR;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Net.Http.Headers;
namespace Grand.Web.Common.Controllers
{
@@ -97,7 +97,7 @@ namespace Grand.Web.Common.Controllers
var workContext = HttpContext.RequestServices.GetRequiredService<IWorkContext>();
var logger = HttpContext.RequestServices.GetRequiredService<ILogger>();
_ = logger.InsertLog(Domain.Logging.LogLevel.Error, exception?.Message, exception?.ToString(), workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
- HttpContext.Request?.GetDisplayUrl(), HttpContext.Request?.Headers[HeaderNames.Referer]);
+ HttpContext.Request?.GetDisplayUrl(), HttpContext.Request?.GetTypedHeaders().Referer?.ToString());
}
/// <summary>
diff --git a/src/Web/Grand.Web.Common/Filters/CustomerActivityAttribute.cs b/src/Web/Grand.Web.Common/Filters/CustomerActivityAttribute.cs
index b589aedb0..5945c09a6 100644
--- a/src/Web/Grand.Web.Common/Filters/CustomerActivityAttribute.cs
+++ b/src/Web/Grand.Web.Common/Filters/CustomerActivityAttribute.cs
@@ -115,23 +115,21 @@ namespace Grand.Web.Common.Filters
if (!pageUrl.Equals(previousPageUrl, StringComparison.OrdinalIgnoreCase))
await _userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.LastVisitedPage, pageUrl);
- if (!string.IsNullOrEmpty(context.HttpContext.Request.Headers[HeaderNames.Referer]))
- if (!context.HttpContext.Request.Headers[HeaderNames.Referer].ToString().Contains(context.HttpContext.Request.Host.ToString()))
+ if (context.HttpContext.Request?.GetTypedHeaders().Referer?.ToString() is { } referer && !referer.Contains(context.HttpContext.Request.Host.ToString()))
+ {
+ var previousUrlReferrer = await _workContext.CurrentCustomer.GetUserField<string>(_userFieldService, SystemCustomerFieldNames.LastUrlReferrer);
+ if (previousUrlReferrer != referer)
{
- var previousUrlReferrer = _workContext.CurrentCustomer.GetUserField<string>(_userFieldService, SystemCustomerFieldNames.LastUrlReferrer);
- var actualUrlReferrer = context.HttpContext.Request.Headers[HeaderNames.Referer];
- if (previousUrlReferrer != actualUrlReferrer)
- {
- await _userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.LastUrlReferrer, actualUrlReferrer);
- }
+ await _userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.LastUrlReferrer, referer);
}
+ }
if (_customerSettings.SaveVisitedPage)
{
if (!_workContext.CurrentCustomer.IsSearchEngineAccount())
{
//activity
- _ = _customerActivityService.InsertActivity("PublicStore.Url", pageUrl, _workContext.CurrentCustomer,
+ _ = _customerActivityService.InsertActivity("PublicStore.Url", pageUrl, _workContext.CurrentCustomer,
context.HttpContext?.Connection?.RemoteIpAddress?.ToString(), pageUrl);
//action
await _customerActionEventService.Url(_workContext.CurrentCustomer, context.HttpContext?.Request?.Path.ToString(), context.HttpContext?.Request?.Headers["Referer"]);
diff --git a/src/Web/Grand.Web.Common/Infrastructure/ApplicationBuilderExtensions.cs b/src/Web/Grand.Web.Common/Infrastructure/ApplicationBuilderExtensions.cs
index 0674808f2..21047d876 100644
--- a/src/Web/Grand.Web.Common/Infrastructure/ApplicationBuilderExtensions.cs
+++ b/src/Web/Grand.Web.Common/Infrastructure/ApplicationBuilderExtensions.cs
@@ -79,7 +79,7 @@ namespace Grand.Web.Common.Infrastructure
customer: workContext.CurrentCustomer,
ipAddress: context.Connection?.RemoteIpAddress?.ToString(),
pageurl: context.Request?.GetDisplayUrl(),
- referrerUrl: context.Request?.Headers[HeaderNames.Referer]);
+ referrerUrl: context.Request?.GetTypedHeaders().Referer?.ToString());
//log error
_ = logger.Error(exception.Message, exception, workContext.CurrentCustomer);
}
@@ -127,7 +127,7 @@ namespace Grand.Web.Common.Infrastructure
customer: workContext.CurrentCustomer,
ipAddress: context.HttpContext?.Connection?.RemoteIpAddress?.ToString(),
pageurl: context.HttpContext?.Request?.GetDisplayUrl(),
- referrerUrl: context.HttpContext?.Request?.Headers[HeaderNames.Referer]);
+ referrerUrl: context.HttpContext?.Request?.GetTypedHeaders().Referer?.ToString());
}
}
await Task.CompletedTask;
@@ -155,7 +155,7 @@ namespace Grand.Web.Common.Infrastructure
_ = logger.InsertLog(Domain.Logging.LogLevel.Error, "Error 400. Bad request", null, customer: workContext.CurrentCustomer,
ipAddress: context.HttpContext?.Connection?.RemoteIpAddress?.ToString(),
pageurl: context.HttpContext?.Request?.GetDisplayUrl(),
- referrerUrl: context.HttpContext?.Request?.Headers[HeaderNames.Referer]);
+ referrerUrl: context.HttpContext?.Request?.GetTypedHeaders().Referer?.ToString());
}
}
return Task.CompletedTask;
diff --git a/src/Web/Grand.Web.Common/StoreHelper.cs b/src/Web/Grand.Web.Common/StoreHelper.cs
index fbac728a0..da3a8a10e 100644
--- a/src/Web/Grand.Web.Common/StoreHelper.cs
+++ b/src/Web/Grand.Web.Common/StoreHelper.cs
@@ -90,7 +90,7 @@ namespace Grand.Web.Common
if (_cachedDomainHost == null)
{
//try to determine the current HOST header
- string host = _httpContextAccessor.HttpContext?.Request?.Headers[HeaderNames.Host];
+ var host = _httpContextAccessor.HttpContext?.Request?.GetTypedHeaders().Host.ToString();
if (StoreHost != null)
{
_cachedDomainHost = StoreHost.HostValue(host) ?? new DomainHost() {
diff --git a/src/Web/Grand.Web.Common/WorkContext.cs b/src/Web/Grand.Web.Common/WorkContext.cs
index da6b4ec83..9be917f3b 100644
--- a/src/Web/Grand.Web.Common/WorkContext.cs
+++ b/src/Web/Grand.Web.Common/WorkContext.cs
@@ -15,7 +15,6 @@ using Grand.Infrastructure.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
-using Microsoft.Net.Http.Headers;
using Wangkanai.Detection.Services;
namespace Grand.Web.Common
@@ -214,7 +213,7 @@ namespace Grand.Web.Common
{
//remove cookie
await _authenticationService.SetCustomerGuid(Guid.Empty);
-
+
//set if use impersonated session
var impersonatedCustomer = await SetImpersonatedCustomer(customer);
if (impersonatedCustomer != null)
@@ -274,10 +273,9 @@ namespace Grand.Web.Common
{
//create guest if not exists
customer = await _customerService.InsertGuestCustomer(CurrentStore);
- string referrer = _httpContextAccessor?.HttpContext?.Request?.Headers[HeaderNames.Referer];
- if (!string.IsNullOrEmpty(referrer))
+ if (_httpContextAccessor?.HttpContext?.Request?.GetTypedHeaders().Referer?.ToString() is { } referer)
{
- await _userFieldService.SaveField(customer, SystemCustomerFieldNames.UrlReferrer, referrer);
+ await _userFieldService.SaveField(customer, SystemCustomerFieldNames.UrlReferrer, referer);
}
//set customer cookie
await _authenticationService.SetCustomerGuid(customer.CustomerGuid);
diff --git a/src/Web/Grand.Web/Controllers/CatalogController.cs b/src/Web/Grand.Web/Controllers/CatalogController.cs
index d286628be..bc7d7dc40 100644
--- a/src/Web/Grand.Web/Controllers/CatalogController.cs
+++ b/src/Web/Grand.Web/Controllers/CatalogController.cs
@@ -26,7 +26,7 @@ using Grand.Web.Models.Vendors;
using MediatR;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.Net.Http.Headers;
+using Microsoft.AspNetCore.Http;
namespace Grand.Web.Controllers
{
@@ -145,7 +145,7 @@ namespace Grand.Web.Controllers
_ = _customerActivityService.InsertActivity("PublicStore.ViewCategory", category.Id,
_workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.PublicStore.ViewCategory"), category.Name);
- await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.Headers[HeaderNames.Referer].ToString() != null ? Request.Headers["Referer"].ToString() : "");
+ await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.GetTypedHeaders().Referer?.ToString());
//model
var model = await _mediator.Send(new GetCategory()
@@ -200,7 +200,7 @@ namespace Grand.Web.Controllers
_ = _customerActivityService.InsertActivity("PublicStore.ViewBrand", brand.Id,
_workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.PublicStore.ViewBrand"), brand.Name);
- await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.Headers[HeaderNames.Referer].ToString() != null ? Request.Headers[HeaderNames.Referer].ToString() : "");
+ await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.GetTypedHeaders().Referer?.ToString());
//model
var model = await _mediator.Send(new GetBrand()
@@ -266,7 +266,7 @@ namespace Grand.Web.Controllers
_ = _customerActivityService.InsertActivity("PublicStore.ViewCollection", collection.Id,
_workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.PublicStore.ViewCollection"), collection.Name);
- await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.Headers[HeaderNames.Referer].ToString() != null ? Request.Headers[HeaderNames.Referer].ToString() : "");
+ await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.GetTypedHeaders().Referer?.ToString());
//model
var model = await _mediator.Send(new GetCollection()
diff --git a/src/Web/Grand.Web/Controllers/CourseController.cs b/src/Web/Grand.Web/Controllers/CourseController.cs
index 017b563cd..8252d9942 100644
--- a/src/Web/Grand.Web/Controllers/CourseController.cs
+++ b/src/Web/Grand.Web/Controllers/CourseController.cs
@@ -14,7 +14,7 @@ using Grand.Web.Features.Models.Courses;
using MediatR;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.Net.Http.Headers;
+using Microsoft.AspNetCore.Http;
namespace Grand.Web.Controllers
{
@@ -115,7 +115,7 @@ namespace Grand.Web.Controllers
_ = _customerActivityService.InsertActivity("PublicStore.ViewCourse", course.Id,
_workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.PublicStore.ViewCourse"), course.Name);
- await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.Headers[HeaderNames.Referer].ToString() != null ? Request.Headers["Referer"].ToString() : "");
+ await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.GetTypedHeaders().Referer?.ToString());
//model
var model = await _mediator.Send(new GetCourse() {
@@ -152,7 +152,7 @@ namespace Grand.Web.Controllers
_ = _customerActivityService.InsertActivity("PublicStore.ViewLesson", lesson.Id,
_workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.PublicStore.ViewLesson"), lesson.Name);
- await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.Headers[HeaderNames.Referer].ToString() != null ? Request.Headers["Referer"].ToString() : "");
+ await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.GetTypedHeaders().Referer?.ToString());
//model
var model = await _mediator.Send(new GetLesson() {
diff --git a/src/Web/Grand.Web/Controllers/PixelController.cs b/src/Web/Grand.Web/Controllers/PixelController.cs
index 5c58b6588..a60e35f9e 100644
--- a/src/Web/Grand.Web/Controllers/PixelController.cs
+++ b/src/Web/Grand.Web/Controllers/PixelController.cs
@@ -2,6 +2,7 @@
using Grand.Infrastructure;
using Grand.Web.Common.Controllers;
using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
@@ -17,7 +18,7 @@ namespace Grand.Web.Controllers
{
if (!string.IsNullOrEmpty(emailId))
{
- if (!Request.Headers[HeaderNames.Referer].ToString().ToLowerInvariant().Contains("admin/queuedemail/edit/".ToLowerInvariant()))
+ if (!(Request.GetTypedHeaders().Referer?.ToString() is { } referer && referer.ToLowerInvariant().Contains("admin/queuedemail/edit/".ToLowerInvariant())))
{
var eueuedEmail = await queuedEmailService.GetQueuedEmailById(emailId);
if (eueuedEmail != null && !eueuedEmail.ReadOnUtc.HasValue)
diff --git a/src/Web/Grand.Web/Controllers/ProductController.cs b/src/Web/Grand.Web/Controllers/ProductController.cs
index e771b69fb..d47362efb 100644
--- a/src/Web/Grand.Web/Controllers/ProductController.cs
+++ b/src/Web/Grand.Web/Controllers/ProductController.cs
@@ -24,7 +24,6 @@ using Grand.Web.Models.Catalog;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.Net.Http.Headers;
using Grand.Web.Common.Extensions;
namespace Grand.Web.Controllers
@@ -172,7 +171,7 @@ namespace Grand.Web.Controllers
//activity log
_ = _customerActivityService.InsertActivity("PublicStore.ViewProduct", product.Id, _workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.PublicStore.ViewProduct"), product.Name);
- await _customerActionEventService.Viewed(customer, this.HttpContext.Request.Path.ToString(), Request.Headers[HeaderNames.Referer].ToString() != null ? Request.Headers[HeaderNames.Referer].ToString() : "");
+ await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.GetTypedHeaders().Referer?.ToString());
await _productService.UpdateMostView(product);
return View(productLayoutViewPath, model);
@@ -419,7 +418,7 @@ namespace Grand.Web.Controllers
//activity log
_ = _customerActivityService.InsertActivity("PublicStore.ViewProduct", product.Id, _workContext.CurrentCustomer, HttpContext.Connection?.RemoteIpAddress?.ToString(),
_translationService.GetResource("ActivityLog.PublicStore.ViewProduct"), product.Name);
- await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.Headers[HeaderNames.Referer].ToString() != null ? Request.Headers[HeaderNames.Referer].ToString() : "");
+ await _customerActionEventService.Viewed(customer, HttpContext.Request.Path.ToString(), Request.GetTypedHeaders().Referer?.ToString());
await _productService.UpdateMostView(product);
return Json(new