Resolve issue where visitor cookie was not being added to HttpClient. This was because cookie values cannot contain spaces and therefore need to be Url encoded.

This commit is contained in:
sbwalker
2025-08-01 07:51:58 -04:00
parent 4c2960eeae
commit 9ae12ff678
3 changed files with 19 additions and 9 deletions

View File

@ -298,7 +298,7 @@ namespace Oqtane.Controllers
if (!authorized) if (!authorized)
{ {
var visitorCookieName = Constants.VisitorCookiePrefix + _alias.SiteId.ToString(); var visitorCookieName = Constants.VisitorCookiePrefix + _alias.SiteId.ToString();
authorized = (entityId == GetVisitorCookieId(Request.Cookies[visitorCookieName])); authorized = (entityId == GetVisitorCookieId(HttpContext.Request.Cookies[visitorCookieName]));
} }
break; break;
default: // custom entity default: // custom entity
@ -352,9 +352,14 @@ namespace Oqtane.Controllers
private int GetVisitorCookieId(string visitorCookie) private int GetVisitorCookieId(string visitorCookie)
{ {
// visitor cookies contain the visitor id and an expiry date separated by a pipe symbol var visitorId = -1;
visitorCookie = (visitorCookie.Contains("|")) ? visitorCookie.Split('|')[0] : visitorCookie; if (visitorCookie != null)
return (int.TryParse(visitorCookie, out int visitorId)) ? visitorId : -1; {
// visitor cookies now contain the visitor id and an expiry date separated by a pipe symbol
visitorCookie = (visitorCookie.Contains("|")) ? visitorCookie.Split('|')[0] : visitorCookie;
visitorId = int.TryParse(visitorCookie, out int _visitorId) ? _visitorId : -1;
}
return visitorId;
} }
private void AddSyncEvent(string EntityName, int EntityId, int SettingId, string Action) private void AddSyncEvent(string EntityName, int EntityId, int SettingId, string Action)

View File

@ -77,9 +77,14 @@ namespace Oqtane.Controllers
private int GetVisitorCookieId(string visitorCookie) private int GetVisitorCookieId(string visitorCookie)
{ {
// visitor cookies contain the visitor id and an expiry date separated by a pipe symbol var visitorId = -1;
visitorCookie = (visitorCookie.Contains("|")) ? visitorCookie.Split('|')[0] : visitorCookie; if (visitorCookie != null)
return (int.TryParse(visitorCookie, out int visitorId)) ? visitorId : -1; {
// visitor cookies now contain the visitor id and an expiry date separated by a pipe symbol
visitorCookie = (visitorCookie.Contains("|")) ? visitorCookie.Split('|')[0] : visitorCookie;
visitorId = int.TryParse(visitorCookie, out int _visitorId) ? _visitorId : -1;
}
return visitorId;
} }
} }
} }

View File

@ -257,7 +257,7 @@ namespace Microsoft.Extensions.DependencyInjection
// set the cookies to allow HttpClient API calls to be authenticated // set the cookies to allow HttpClient API calls to be authenticated
foreach (var cookie in httpContextAccessor.HttpContext.Request.Cookies) foreach (var cookie in httpContextAccessor.HttpContext.Request.Cookies)
{ {
client.DefaultRequestHeaders.Add("Cookie", cookie.Key + "=" + cookie.Value); client.DefaultRequestHeaders.Add("Cookie", cookie.Key + "=" + WebUtility.UrlEncode(cookie.Value));
} }
} }
@ -275,7 +275,7 @@ namespace Microsoft.Extensions.DependencyInjection
// set the cookies to allow HttpClient API calls to be authenticated // set the cookies to allow HttpClient API calls to be authenticated
foreach (var cookie in httpContextAccessor.HttpContext.Request.Cookies) foreach (var cookie in httpContextAccessor.HttpContext.Request.Cookies)
{ {
client.DefaultRequestHeaders.Add("Cookie", cookie.Key + "=" + cookie.Value); client.DefaultRequestHeaders.Add("Cookie", cookie.Key + "=" + WebUtility.UrlEncode(cookie.Value));
} }
} }
}); });