Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Weber 48 posts 123 karma points c-trib
    Feb 02, 2021 @ 10:17
    Jesper Weber
    0

    Personalisation group criteria based on cookie

    Hi I have a personalisation groups that returns true if a cookie matches a specific value, over all this works fine.

    But since cookies are set on the response and the read on the request this means that the criteria is only true on the seconds request and future requests.

    Is it possible to add my own implementation of the HttpContextCookieProvider from the plugin or are the any other ways to handle this situatiuon?

  • Andy Butland 401 posts 2258 karma points MVP 4x hq c-trib
    Feb 02, 2021 @ 11:06
    Andy Butland
    0

    Hi - I've not made this class pluggable no, but if you can find a way to handle the situation you've described by updating the existing class - source code here - via a PR (or just suggestions) I'll be happy to try to include it.

  • Jesper Weber 48 posts 123 karma points c-trib
    Feb 02, 2021 @ 12:09
    Jesper Weber
    0

    Hi Andy,

    Thanks for your reply.

    What I usually do when I need this functionality is to create a CookieService og CookieProvider like you have.

    It will have to methods, SetCookie and GetCookie (and CookieExists if needed).

    The SetCookie method will set the cookie on the response and set the value in the HttpContext.Current.Items which exists only in the current request.

    The GetCookie will then start by looking in the HttpContext.Current.Items and if no value with the specified key exists it looks for the cookie.

    This way you can set a cookie in a request and get the "cookie" value in the same request.

    I don't know if this is the preferred way by default in the plugin, but if the HttpContextCookieProvider was pluggable by registering a new implementation of the interface in the IOC container, I could do this in my specific solution.

  • Andy Butland 401 posts 2258 karma points MVP 4x hq c-trib
    Feb 02, 2021 @ 14:59
    Andy Butland
    0

    It sounds to me that what you've described should likely be the default behaviour, so I'll take a look at implementing that when I get a bit of time. Or if you fancy PRing it, I'll be happy to review and include.

    Andy

  • Jesper Weber 48 posts 123 karma points c-trib
    Feb 04, 2021 @ 09:36
    Jesper Weber
    0

    Hi Andy, I have created an example of a cookie provider. But with this users are forced to use this class to set cookies (so that the value is also set in the HttpContext.Current.Items with the right key), before they could set the cookie in the javascript or in whatever way they find best.

    Maybe I think it's better if the HttpContextCookieProvider class and the ICookieProvider interface was registered in the IOC container, so users could simply register they own implementation of the interface?

    public class HttpContextCookieProvider : ICookieProvider
    {
        public bool CookieExists(string key)
        {
            return HttpContext.Current.Items.Contains($"personalisationGroups.cookie.{key}") || HttpContext.Current.Request.Cookies[key] != null;
        }
    
        public string GetCookieValue(string key)
        {
            if (HttpContext.Current.Items.Contains($"personalisationGroups.cookie.{key}"))
                return HttpContext.Current.Items[$"personalisationGroups.cookie.{key}"].ToString();
    
            return HttpContext.Current.Request.Cookies[key]?.Value;
        }
    
        public void SetCookie(HttpCookie cookie)
        {
            HttpContext.Current.Items[$"personalisationGroups.cookie.{cookie.Name}"] = cookie.Value;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
    
  • Andy Butland 401 posts 2258 karma points MVP 4x hq c-trib
    Feb 04, 2021 @ 10:37
    Andy Butland
    0

    Thanks for this Jesper, this looks good to me.

    On the IoC container point, of course you're fundamentally right and I agree that would be the best way to go.

    The only concern I've got though is that this package also supports Umbraco V7 and I'm not sure if the IoC container support was there in core back in that version. I recall as a developer you could certainly introduce your own IoC container, but I'm not sure core shipped with one, and hence as a package developer you can't know what will be available.

    So IIRC that's why I'm currently just "newing up" these classes in the code, and have a "poor man's DI" type of thing with the alternative constructor taking the interface purely to support unit testing.

    Because of that I'm minded to just take and use your implementation - as it seems better to me, resolving the original issue you've raised - and just to be safe, introduce a config switch allowing people to opt-out of using the HttpContext.Items collection as well should they for some reason want to (and hence have the current behaviour).

    Thoughts?

    Andy

  • Jesper Weber 48 posts 123 karma points c-trib
    Feb 04, 2021 @ 10:56
    Jesper Weber
    0

    Hi Andy, I haven't thought about the V7 support and based on that, I agree with the thoughts you have and you are more than welcome to take my implementation.

  • Andy Butland 401 posts 2258 karma points MVP 4x hq c-trib
    Feb 04, 2021 @ 11:08
    Andy Butland
    0

    Thanks Jesper... and apologies, I realise my comment to be "minded to just take and use your implementation" is a little presumptious on reading back!

    But that's great, I'll get this introduced and a release out - hopefully over the weekend.

    Andy

  • Jesper Weber 48 posts 123 karma points c-trib
    Feb 04, 2021 @ 12:08
    Jesper Weber
    0

    No worries, that was not how I perceived it, I'm happy to help out and thanks for taking the time to create a new release :-)

  • Andy Butland 401 posts 2258 karma points MVP 4x hq c-trib
    Feb 06, 2021 @ 08:58
    Andy Butland
    100

    I've created a release now with this included - versions 1.0.6 (for V7) and 2.1.5 (for V8). These are on our and NuGet.

    Andy

  • Jesper Weber 48 posts 123 karma points c-trib
    Feb 08, 2021 @ 14:10
    Jesper Weber
    0

    Thanks Andy, I have tested and it's working as expected.

Please Sign in or register to post replies

Write your reply to:

Draft