Recent changes in .NET and Google Chrome can result in broken PDFs when viewing the PDF in Google Chrome. This will affect all Asp.Net based applications using Forms Authentication with larger PDFs that require authentication to download them. In this blog post we'll discuss the cause of this - but also what can be done to remedy the issue.
We had a customer that recently started to experience problems with PDF:s on an intranet where some PDFs did not load correctly in the browser. After some investigation we found that this only happened in Google Chrome – and only for larger documents. When comparing with a few other Episerver based solutions we found that the following criteria would create the issue:
- The document had to be big enough to trigger chunked download in the browser (which happens around roughly 2 Mb in Episerver).
- The document should not be available to everyone.
- The solution uses Forms based authentication (with Asp.Net Identity it works as expected).
While tracking the bug – I found out that Chrome seems to handle range requests slightly differently than for instance Edge and Firefox. Also – Chrome does not show chunked requests as several requests in the browser debugger – this is only visible in a HTTP debugger tool like Fiddler. In Fiddler I could see the following behaviour:
Initial request in Chrome
Cookies: All cookies are part of the request
Subsequent requests (range download requests)
Cookies: Cookies that are marked as samesite = LAX are not part of these requests
When comparing some working sites with the site that had this issue – I could notice that the site with the problem had a cookie named ".EpiserverLogin" that had samesite = LAX. This cookie is the default configured name for Forms based authentication in Episerver - using Asp.Net Forms Authentication. For the working sites – the corresponding cookie with credentials is called ".AspNet.ApplicationCookie" – and this cookie did not have the corresponding samesite information. It turns out that the site with the issue uses Forms based Authentication compared to the other sites that uses Asp.Net Identity.
Since I had read a bit about the changes in Chrome related to the samesite attribute for cookies – I expected that this was the cause. However – I could experience the issue in Chrome 79 – and the changes in Chrome was first launched in version 80 that was released last week. However – I guessed that something other than Chrome did some related changes prior to the big change in Google Chrome. After some more investigation, I found that there was a security patch to .NET 3.5 and 4.7.2 that changes the samesite value for Forms authentication and Session state to LAX.
This patch has been rolled out in Microsoft Azure App services during January this year:
What do I do if I'm affected by this change?
- Specifically configure forms authentication to set samesite = None in web.config. Notice that it's strongly recommended to set this cookie as secure when doing this – which requires HTTPS to be set up – even for your developer environment.
<forms cookieSameSite="None" requireSSL="true" name=".EPiServerLogin" />
Information about this can be found here: https://docs.microsoft.com/en-us/aspnet/samesite/system-web-samesite
- If this is only affecting a few PDF:s on your solution – there's a trick in Episerver to trigger the browser to download the PDF rather than opening it inside the browser. This avoids chunked download and thus the problem. This can be done by adding "/download" in the remaining URL input field in Episervers link dialog. Notice that this will affect all browsers – even browsers that does not have the issue.
Except of the links in the article - I recommend the following article to understand more about the changes in .NET related to samesite - since this has effects to other things - for instance authentication with OpenIdConnect
Update 1: There already seems to be a bug report in Chromium regarding this bug - though it has not been prioritized yet. See https://bugs.chromium.org/p/chromium/issues/detail?id=961617