0

I am having trouble with a script I am writing that is using System.Net.WebClient (called from Powershell but I guess the problem should occur with everything that is using the same cache as System.Net.WebRequest):

For context (as there may be a better solution than what I found):

  • I made an extension for IE (yes, some clients still use it) in C# (yes, it's not recommended but I had no choice)
  • this extension needs to run with EPM activated (so low-privileged).
  • it needs a configuration file that is available on a server accessed by HTTPS.
  • the configuration needs to be available when IE is launched so we have to cache it (also, each tab has its own instance of the extension)
  • that cached configuration have to stay in a privileged folder (the extension injects code to some of the pages according to that configuration, so you don't want the user or any process to have write access to it)

To solve the problem of caching the configuration, I wrote a Powershell script that is launched through the task scheduler. The script uses System.Net.WebClient to download the file, and I set it to respect the cache of the file:

$webclient = New-Object System.Net.WebClient
$cacheLevel = [System.Net.Cache.RequestCacheLevel]::CacheIfAvailable
$webclient.CachePolicy = New-Object System.Net.Cache.RequestCachePolicy($cacheLevel)

When I launch the script using "Run As Administrator", the cache is respected (providing the server is well configured).

When I launch the script from the task scheduler (user NT AUTHORITY\SYSTEM, as I need privilege to be able to save the file in the extension installation dir), the cache is not respected and the file is downloaded every single time.

Any idea on how to solve this issue? I need the caching to able to be poll the file without having to do a full download (the file is small, but the number of users is high :D).

Maybe it would be possible to use the date of the file that was previously downloaded?

consta_a
  • 31
  • 7
  • How did you determine that the cache works under one login and not the other? – Mathias R. Jessen Mar 30 '20 at 10:12
  • WebClient doesn't use IE *under the hood*. It uses the RequestCachePolicy to setup the underlying WebRequest (which doesn't use IE or the `WinInet` cache). – Jimi Mar 30 '20 at 10:52
  • @MathiasR.Jessen: I setup a server that serves the configuration file as a JSP which outputs some logs. When I launch the script by hand, the log is not output during the duration of the cache, while it is output each time I launch the task on the task scheduler. – consta_a Mar 30 '20 at 11:51
  • @Jimi: When launching the script by hand after emptying IE's cache, I can see the file is downloading again. This [answer](https://stackoverflow.com/a/11061136/867942) also shows they are somewhat related. – consta_a Mar 30 '20 at 11:57
  • The cache repository location is a different matter. My comment is about the *relation* between WebClient and IE, in reference to the *with everything that is using IE under the hood* statement. – Jimi Mar 30 '20 at 12:01
  • I understand, I edited my question to reflect your comment. A colleague of mine tested it and he has differences when he tests on the 2 accounts on his PC: for one account emptying the IE's cache empties the cache used by WebClient, while in the other account it does not... "funny" I guess? – consta_a Mar 30 '20 at 13:22
  • The cache location is determined by the user. If the current logged in user clears the local cache, it doesn't clear the cache of all users. – Jimi Mar 30 '20 at 13:51
  • @Jimi: Yes, this I know. The test that was done was the same for each user: download through the script, clear the cache of IE, download again through the script. On one account the file downloaded again, on the other one the file came from cache while IE's cache had been emptied. This would imply that for one user the cache is the same for IE and WebRequest while for the other user not. – consta_a Mar 30 '20 at 15:57

0 Answers0