-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Application Insights front-end telemetry (#1085)
* Variable name change to be more explicit * Fixed broken src tag * Define the users identifier * Set app config value for toggling browser analytics * Moved iframe into body tag * Added App Insights Browser JS * Use identity name as email claim is not set * Corrected EoL feed * Added App Insights to CSP * Updated GA script to match shared Layout.cshtml * Remove unused injection * Add App Insights SDK to layout variant * Updated Cookie Policy statement * Corrected line ending * Expire App Insights cookies on rejection choice * Build pipeline for minifying App Insights JS
- Loading branch information
1 parent
b59420a
commit 3ad6163
Showing
10 changed files
with
624 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 112 additions & 105 deletions
217
Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Public/CookiePreferences.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,112 @@ | ||
using Dfe.PrepareConversions.Configuration; | ||
using Dfe.PrepareConversions.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
|
||
namespace Dfe.PrepareConversions.Pages.Public; | ||
|
||
public class CookiePreferences : PageModel | ||
{ | ||
private const string CONSENT_COOKIE_NAME = ".ManageAnAcademyConversion.Consent"; | ||
private readonly ILogger<CookiePreferences> _logger; | ||
private readonly IOptions<ServiceLinkOptions> _options; | ||
|
||
public CookiePreferences(ILogger<CookiePreferences> logger, IOptions<ServiceLinkOptions> options) | ||
{ | ||
_logger = logger; | ||
_options = options; | ||
} | ||
|
||
public bool? Consent { get; set; } | ||
public bool PreferencesSet { get; set; } | ||
public string ReturnPath { get; set; } | ||
|
||
public string TransfersCookiesUrl { get; set; } | ||
|
||
public ActionResult OnGet(bool? consent, string returnUrl) | ||
{ | ||
ReturnPath = returnUrl; | ||
TransfersCookiesUrl = $"{_options.Value.TransfersUrl}/cookie-preferences?returnUrl=%2Fhome"; | ||
|
||
if (Request.Cookies.ContainsKey(CONSENT_COOKIE_NAME)) | ||
{ | ||
Consent = bool.Parse(Request.Cookies[CONSENT_COOKIE_NAME] ?? string.Empty); | ||
} | ||
|
||
if (consent.HasValue) | ||
{ | ||
PreferencesSet = true; | ||
|
||
ApplyCookieConsent(consent); | ||
|
||
if (!string.IsNullOrEmpty(returnUrl)) | ||
{ | ||
return Redirect(returnUrl); | ||
} | ||
|
||
return RedirectToPage(Links.Public.CookiePreferences); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
public IActionResult OnPost(bool? consent, string returnUrl) | ||
{ | ||
ReturnPath = returnUrl; | ||
|
||
if (Request.Cookies.ContainsKey(CONSENT_COOKIE_NAME)) | ||
{ | ||
Consent = bool.Parse(Request.Cookies[CONSENT_COOKIE_NAME] ?? string.Empty); | ||
} | ||
|
||
if (consent.HasValue) | ||
{ | ||
Consent = consent; | ||
PreferencesSet = true; | ||
|
||
CookieOptions cookieOptions = new() { Expires = DateTime.Today.AddMonths(6), Secure = true, HttpOnly = true }; | ||
Response.Cookies.Append(CONSENT_COOKIE_NAME, consent.Value.ToString(), cookieOptions); | ||
|
||
if (consent.Value is false) | ||
{ | ||
ApplyCookieConsent(false); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
private void ApplyCookieConsent(bool? consent) | ||
{ | ||
if (consent.HasValue) | ||
{ | ||
CookieOptions cookieOptions = new() { Expires = DateTime.Today.AddMonths(6), Secure = true, HttpOnly = true }; | ||
Response.Cookies.Append(CONSENT_COOKIE_NAME, consent.Value.ToString(), cookieOptions); | ||
} | ||
|
||
if (consent is false) | ||
{ | ||
foreach (string cookie in Request.Cookies.Keys) | ||
{ | ||
if (cookie.StartsWith("_ga") || cookie.Equals("_gid")) | ||
{ | ||
_logger.LogInformation("Expiring Google analytics cookie: {cookie}", cookie); | ||
Response.Cookies.Append(cookie, string.Empty, new CookieOptions { Expires = DateTime.Now.AddDays(-1), Secure = true, SameSite = SameSiteMode.Lax, HttpOnly = true }); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
using Dfe.PrepareConversions.Configuration; | ||
using Dfe.PrepareConversions.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
|
||
namespace Dfe.PrepareConversions.Pages.Public; | ||
|
||
public class CookiePreferences : PageModel | ||
{ | ||
private const string CONSENT_COOKIE_NAME = ".ManageAnAcademyConversion.Consent"; | ||
private readonly ILogger<CookiePreferences> _logger; | ||
private readonly IOptions<ServiceLinkOptions> _options; | ||
|
||
public CookiePreferences(ILogger<CookiePreferences> logger, IOptions<ServiceLinkOptions> options) | ||
{ | ||
_logger = logger; | ||
_options = options; | ||
} | ||
|
||
public bool? Consent { get; set; } | ||
public bool PreferencesSet { get; set; } | ||
public string ReturnPath { get; set; } | ||
|
||
public string TransfersCookiesUrl { get; set; } | ||
|
||
public ActionResult OnGet(bool? consent, string returnUrl) | ||
{ | ||
ReturnPath = returnUrl; | ||
TransfersCookiesUrl = $"{_options.Value.TransfersUrl}/cookie-preferences?returnUrl=%2Fhome"; | ||
|
||
if (Request.Cookies.ContainsKey(CONSENT_COOKIE_NAME)) | ||
{ | ||
Consent = bool.Parse(Request.Cookies[CONSENT_COOKIE_NAME] ?? string.Empty); | ||
} | ||
|
||
if (consent.HasValue) | ||
{ | ||
PreferencesSet = true; | ||
|
||
ApplyCookieConsent(consent); | ||
|
||
if (!string.IsNullOrEmpty(returnUrl)) | ||
{ | ||
return Redirect(returnUrl); | ||
} | ||
|
||
return RedirectToPage(Links.Public.CookiePreferences); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
public IActionResult OnPost(bool? consent, string returnUrl) | ||
{ | ||
ReturnPath = returnUrl; | ||
|
||
if (Request.Cookies.ContainsKey(CONSENT_COOKIE_NAME)) | ||
{ | ||
Consent = bool.Parse(Request.Cookies[CONSENT_COOKIE_NAME] ?? string.Empty); | ||
} | ||
|
||
if (consent.HasValue) | ||
{ | ||
Consent = consent; | ||
PreferencesSet = true; | ||
|
||
CookieOptions cookieOptions = new() { Expires = DateTime.Today.AddMonths(6), Secure = true, HttpOnly = true }; | ||
Response.Cookies.Append(CONSENT_COOKIE_NAME, consent.Value.ToString(), cookieOptions); | ||
|
||
if (consent.Value is false) | ||
{ | ||
ApplyCookieConsent(false); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
private void ApplyCookieConsent(bool? consent) | ||
{ | ||
if (consent.HasValue) | ||
{ | ||
CookieOptions cookieOptions = new() { Expires = DateTime.Today.AddMonths(6), Secure = true, HttpOnly = true }; | ||
Response.Cookies.Append(CONSENT_COOKIE_NAME, consent.Value.ToString(), cookieOptions); | ||
} | ||
|
||
if (consent is false) | ||
{ | ||
foreach (string cookie in Request.Cookies.Keys) | ||
{ | ||
// Google Analytics | ||
if (cookie.StartsWith("_ga") || cookie.Equals("_gid")) | ||
{ | ||
_logger.LogInformation("Expiring Google analytics cookie: {cookie}", cookie); | ||
Response.Cookies.Append(cookie, string.Empty, new CookieOptions { Expires = DateTime.Now.AddDays(-1), Secure = true, SameSite = SameSiteMode.Lax, HttpOnly = true }); | ||
} | ||
// App Insights | ||
if (cookie.StartsWith("ai_")) | ||
{ | ||
_logger.LogInformation("Expiring App insights cookie: {cookie}", cookie); | ||
Response.Cookies.Append(cookie, string.Empty, new CookieOptions { Expires = DateTime.Now.AddYears(-1), Secure = true, SameSite = SameSiteMode.Lax, HttpOnly = true }); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.