locked
Unable to track if the user accept the privacy message using HttpContext.Features.Get<ITrackingConsentFeature> RRS feed

  • Question

  • User-540818677 posted

    I am working on an asp.net MVC 3.1 web application,and i follow the steps inside this link to activate showing a privacy message @ https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-3.1 .

    now inside my Action Method, i want to check if the user accept the privacy alert or not, i tried the following code:-

    var consentFeature = HttpContext.Features.Get<ITrackingConsentFeature>();
    var canTrack = !consentFeature?.CanTrack ?? false;

    but the canTrack will always be false, even if the user accepts the privacy alert. any advice on this please? Thanks

    Wednesday, June 3, 2020 1:41 PM

All replies

  • User711641945 posted

    Hi johnjohn123123,

    When I follow the documents and add the code in the Startup.cs.It would not display the privacy alert.It means the following value is false in the _CookieConsentPartial.cshtml:

    var showBanner = !consentFeature?.CanTrack ?? false;

    You said canTrack value which is the same as showBanner always be false even you accept the privacy alert.How did you display the privacy alert because it could only display if canTrack is true.

    I right-click the project and choose properties then find the Debug panel to unchecked the enable ssl. Build the project then it could display the privacy alert:

    Best Regards,

    Rena

    Thursday, June 4, 2020 3:24 AM
  • User-540818677 posted

    When I follow the documents and add the code in the Startup.cs.It would not display the privacy alert.I

    but in my case (asp.net core mvc 3.1) the privacy alert showed.

    .How did you display the privacy alert because it could only display if canTrack is true.

    by following the documentation on this link @ https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-3.1

    I right-click the project and choose properties then find the Debug panel to unchecked the enable ssl. Build the project then it could display the privacy alert:

    in my case i do not have to do this..

    so returning back to my original question, how i can track if the user accepted the privacy alert?

    Thursday, June 4, 2020 8:47 AM
  • User475983607 posted

    I can't reproduce your finding.  The TrackingConsentFeature API works exactly as written in reference documentation.  I wrote a demo below to highlight the options available in the API. 

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.features.itrackingconsentfeature?view=aspnetcore-3.1

    public IActionResult Privacy()
    {
        ITrackingConsentFeature consentFeature = HttpContext.Features.Get<ITrackingConsentFeature>();
        return View(consentFeature);
    }
    @model Microsoft.AspNetCore.Http.Features.ITrackingConsentFeature
    @{
        ViewData["Title"] = "Privacy Policy";
    }
    <h1>@ViewData["Title"]</h1>
    
    <p>Use this page to detail your site's privacy policy.</p>
    
    <div>Consent has been given or if consent is not required: @Model.CanTrack</div>
    <div>Consent was given: @Model.HasConsent</div>
    <div>Consent is required for the given request: @Model.IsConsentNeeded</div>

    Keep in mind that the cookie is set in JavaScript.  One request is required to read the cookie on the server.

    Thursday, June 4, 2020 2:19 PM