1

I am tring to clear caches of client's all browsers using asp.net application on button click. Its working fine when we running it from visual studio. But the problem is when we host our application into IIS, its become not working.

Here is code, I used to clear caches of client's browser

  [WebMethod]
        public static string DoClearCache()
           {
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session["AEmail"] = null;
            HttpContext.Current.Session.Clear();
            ClearCaches();
            clearchachelocalall();
            return null;
           }

    public static void ClearCaches()
    {
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetExpires(DateTime.Now);
        //HttpContext.Current.Response.Cache.SetNoServerCaching();
        //HttpContext.Current.Response.Cache.SetNoStore();
        HttpContext.Current.Response.Cookies.Clear();
        HttpContext.Current.Request.Cookies.Clear();
    }


    private static void clearchachelocalall()
    {
        string GooglePath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Local\Google\Chrome\User Data\Default\";
        string MozilaPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Roaming\Mozilla\Firefox\";
        string Opera1 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Local\Opera\Opera";
        string Opera2 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Roaming\Opera\Opera";
        string Safari1 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Local\Apple Computer\Safari";
        string Safari2 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Roaming\Apple Computer\Safari";
        string IE1 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Local\Microsoft\Intern~1";
        string IE2 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Local\Microsoft\Windows\History";
        string IE3 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Local\Microsoft\Windows\Tempor~1";
        string IE4 = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Roaming\Microsoft\Windows\Cookies";
        string Flash = Environment.GetEnvironmentVariable("USERPROFILE") + @"\AppData\Roaming\Macromedia\Flashp~1";

        //Call This Method ClearAllSettings and Pass String Array Param
        ClearAllSettings(new string[] { GooglePath, MozilaPath, Opera1, Opera2, Safari1, Safari2, IE1, IE2, IE3, IE4, Flash });

    }


    public static void ClearAllSettings(string[] ClearPath)
    {
        foreach (string HistoryPath in ClearPath)
        {
            if (Directory.Exists(HistoryPath))
            {
                DoDelete(new DirectoryInfo(HistoryPath));
            }

        }
    }

    public static void DoDelete(DirectoryInfo folder)
    {
        try
        {

            foreach (FileInfo file in folder.GetFiles())
            {
                try
                {
                    file.Delete();
                }
                catch
                { }

            }
            foreach (DirectoryInfo subfolder in folder.GetDirectories())
            {
                DoDelete(subfolder);
            }
        }
        catch
        {
        }
    }

I'm really looking for your valuable suggestions.

  • This is not gonna work. You do not have access to the `USERPROFILE` of the client pc. For very obvious security reasons. It works in Visual Studio because your local account is running the website, but on the server it is another account. – VDWWD Jun 27 '17 at 11:48
  • Is there any other way to achieve it? – Daniel A Sathish Kumar Jun 27 '17 at 11:49
  • No, servers do not have direct access to your user profile. – VDWWD Jun 27 '17 at 11:50
  • Take a step back, and try and explain to us why you _think_ you need to do this. Then we may be able to help you solve the underlying problem. – mjwills Jun 27 '17 at 12:38
  • @mjwills Sometimes we are facing issues from client side due to old cached data .on that time we are instructing client to clear their caches. Obviously some of them do not have knowledge to clear cache, so every time we need to instruct them to clear their cached data. To avoid this kind of situation we are planning to give clear cache option from our application on single click – Daniel A Sathish Kumar Jun 27 '17 at 13:06
  • Also consider http://getcassette.net/ . Search that page for `cache-breaker` - that is the thing you need to do. – mjwills Jun 27 '17 at 13:11
  • 1
    Sure i will give a try to your suggestion. Thanks – Daniel A Sathish Kumar Jun 27 '17 at 13:14
  • @mjwills its worked for us. – Daniel A Sathish Kumar Jun 28 '17 at 06:45
  • Excellent, good to hear! – mjwills Jun 28 '17 at 09:41
  • @mjwills I have been running on issue with auto versioning the file name. I have added query string with source url ( eg: http://localhost:23976/Styles/Site3.css?v=9328C5016419DEB040A610D4712BD917) and it works fine. But the problem is it seems not cached on client browser. Is there any way to ensure the auto versioned file to be cached? – Daniel A Sathish Kumar Jul 03 '17 at 10:19
  • What makes you think it is not cached? Do you have dev tools open in your browser (often that disables caching)? Have you used Fiddler to confirm whether the request is being made? How are you setting cache headers? Is debug set to false (see http://getcassette.net/documentation/v1/getting-started )? – mjwills Jul 04 '17 at 12:03
  • I just forgotten to set debug false.. Now its working fine. Thanks for your wonderful guidance. Its very much appreciated. – Daniel A Sathish Kumar Jul 05 '17 at 04:58

0 Answers0