+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / UserAgent being cached breaks browser detection

UserAgent being cached breaks browser detection

15 posts, 1 answered
  1. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    31 Mar 2010
    Link to this post
    We currently have caching set up for all of our pages (Page > Properties > Enable Caching checkbox). This causes the browser's user agent to be cached, which is a problem. 

    Steps to reproduce:
    1. Add this to your code behind, so it will show up on a page with caching enabled:
    HtmlGenericControl scriptTag = new HtmlGenericControl("script");
    scriptTag.Attributes.Add("type", "text/javascript");
    scriptTag.InnerText = String.Format(@"alert('{0}');", Request.UserAgent);
    Page.Header.Controls.Add(scriptTag);

    2. In IIS, recycle your application pool.

    3. Visit the page with Chrome. Note the user agent that shows in the alert.

    4. Visit the page with Firefox. Note the user agent that shows in the alert; it's the same as Chrome's.

    I can't understand why the user agent is getting cached; is there a way to shut that off? This seems like a significant bug.
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    01 Apr 2010
    Link to this post
    Hello Connections Academy Developer,

    Thank you for contacting Telerik Support.

    Could you please take a look at the following blog post explaining how to extend caching to cache by different criteria:
    Implement caching by domain name

    If you are using the ASPNET caching provider the cache by default is different for each browser as it also adds the browser type in the cache key. If however you are using the MemoryCache provider the browser type is not added to the cache key. You can override the memory cache provider as in above linked post to add the browser type to the cache key.

    All the best,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    02 Apr 2010
    Link to this post
    By your response, it would seem you're saying (in a roundabout way) it isn't cached by default.

    Regardless, I was able to fix my problem by changing from Request.UserAgent to HttpContext.Current.Request.UserAgent. Seeing the use of HttpContext.Current.Request.UserAgent in your blog post gave me the idea.

    Thanks,

    Michael Snyder
  4. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    29 Apr 2010
    Link to this post
    I'm sorry, this is not fixed. We just had IE and Chrome users see Safari-only content.

    Somehow, page caching is causing this result to cache:

    HttpContext.Current.Request.UserAgent

    Could you please look into this again? 

  5. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    29 Apr 2010
    Link to this post
    More information, we're using ASPNET caching. I'm not sure what change to GetVaryByCustomString would remove the UserAgent from the caching.
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    30 Apr 2010
    Link to this post
    Hello Connections Academy Developer,

    If you are using ASP.NET caching for pages and set the Global.asax like this:
    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom.Equals("cms"))
        {
            return String.Concat(context.Request.Url.Host, context.Request.RawUrl, context.Request.UserAgent).ToLower();
        }
        return base.GetVaryByCustomString(context, custom);
    }

    Then the pages will be cached based on the UserAgent. Please take a look at the attached screen cast where I browse a cached page with 3 different browsers and the alert is showing the proper agent.

    Greetings,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  7. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    30 Apr 2010
    Link to this post
    Radoslav,

    Thanks for the prompt reply and your example. I have a good feeling about this, I'm going to give it a test run, and I'll let you know how it works.

    Thanks,

    Michael Snyder
  8. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    30 Apr 2010
    Link to this post
    One more question. Our global.asax file has this override of GetVaryByCustomString:

    public override string GetVaryByCustomString(HttpContext context, string custom) {
        if (custom.Equals("cms", StringComparison.CurrentCultureIgnoreCase)) {
            Telerik.Cms.Web.ICmsUrlContext urlContext = Telerik.Cms.Web.CmsUrlContext.Current;
            if (urlContext != null) {
                    return HttpContext.Current.Request.RawUrl;
            }
        }
        return base.GetVaryByCustomString(context, custom);
    }

    This has been in place since we first installed Sitefinity (v3.6). Will there be any problem if I replace it with what you specified? If there a combination of both versions I should be using?

    Thanks, 

    Michael Snyder
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 May 2010
    Link to this post
    Hi Connections Academy Developer,

    Thank you for getting back to me.

    There should be no problems changing the code in Global.asax. In newer versions of Sitefinity we cache by host., raw URL, and browser type.

    Greetings,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  10. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    12 May 2010
    Link to this post
    Hi Rado,

    Just for clarification, when you say "browser type" what exactly do you mean? User agent? Vendor? Vendor and Major Version?

    Thanks,

    Michael Snyder
  11. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    12 May 2010
    Link to this post
    Hello Connections Academy Developer,

    We by browser type I mean what the current request caries - HttpContext.Request.Browser.Type property. However as I said you can modify this to substitute with Request.UserAgent. You can find more information on what both properties carry as value here:
    HttpRequest.Browser Property
    HttpRequest.UserAgent Property

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  12. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    19 May 2010
    Link to this post
    Thanks for all of your help with this. I only have one more question.

    Since I'm implementing the GetVaryByCustomString as you recommended, how would I exclude a file from being cached? For example, I have /foo/hello.ashx that I never want cached. Is there a way to exclude it from the cache?
  13. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    19 May 2010
    Link to this post
    Hi Connections Academy Developer,

    You have a page which you do not want cached you should go to its properties section and uncheck the Enable Caching checkbox.

    Greetings,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  14. Connections Academy Developer
    Connections Academy Developer avatar
    151 posts
    Registered:
    04 May 2005
    19 May 2010
    Link to this post
    Sorry, I wasn't clear. /foo/hello.ashx is a file in the project; it isn't a part of Sitefinity.

    Since the implications of adding the useragent to the cache key aren't currently known, we're having a handler load the CSS based on the user agent. The problem is, if the handler's cached, this won't work.
  15. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    20 May 2010
    Link to this post
    Hi Connections Academy Developer,

    By default requests to this handler will not be cached by the CMS. The only reason that this would be cached is if your code has some caching settings. We do not cache external resources such as handler requests.

    All the best,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
15 posts, 1 answered