How to identify \ Temporary ASP.NET files \ root \ {site hash} using PowerShell?

ASP.NET uses a temporary file directory to store files for Shadow Copying and Dynamic Compilation. A typical path would look like this. Note the hash at the end of the path.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\{hash}

I use automatic deployment and noticed that these folders and their contents are not deleted automatically. I would like to automate the process of deleting unused temp files as part of my deployment process. Thus, as soon as a new version of my site is deployed, the old temporary files are deleted.

It seems that the hash is generated in a deterministic way, so I hope I can identify the hash as part of my pre-deployment script and delete it after deployment.

How is the hash calculated for an ASP.NET website?

Literature:

+4
source share
2 answers

Ok, a few seconds with Reflector, searching for all the members in System.Web for the word “Temporary” shows that this call calls me here:

string str2 = AppManagerAppDomainFactory.ConstructSimpleAppName(AppDomainAppVirtualPath);

But then I remember that the source code is available for all .NET, so I go to the actual code, not the reflex code: http://referencesource.microsoft.com/#System.Web/xsp/system/Web/HttpRuntime. cs # 872

"SetupCodeGenDirectory" , -,

 this._codegenDir = Thread.GetDomain().DynamicDirectory;

, , DynamicDirectory. http://referencesource.microsoft.com/#mscorlib/system/appdomain.cs#2792, extern, COM Fusion Loader:

[ComImport,InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("7c23ff90-33af-11d3-95da-00a024a85b51")]

GUID http://referencesource.microsoft.com/#mscorlib/microsoft/win32/fusionwrap.cs#94

, :

     public const uint HASH_VALUE            = PUBLIC_KEY_TOKEN + 1;
     public const uint NAME                  = HASH_VALUE + 1;

, NAME? , yourAssembly.GetPublicKeyToken();

, , ASP.NET http://msdn.microsoft.com/en-us/library/system.web.configuration.compilationsection.tempdirectory%28VS.80%29.aspx system.web .

<compilation tempDirectory="D:\MoveThemHere">
+3

, , , - , #, . , powershell.

        string siteId = "/LM/W3SVC/3/ROOT"; // This can be composed by getting the site ID using the ServerManager (Microsoft.Web.Administration)
        string physicalPath = "E:\\home\\mysite\\web\\";

        string v_app_name = HashCode.GetString32((siteId + physicalPath).ToLower(CultureInfo.InvariantCulture)).ToString("x", CultureInfo.InvariantCulture);

        string dir_name_32_bits_app = HashCode.GetString32(v_app_name).ToString("x8");
        string dir_name_64_bits_app = HashCode.GetString64(v_app_name).ToString("x8");

        Console.WriteLine("32 bits: " + dir_name_32_bits_app);
        Console.WriteLine("64 bits: " + dir_name_64_bits_app);

HashCode:

public class HashCode
{        
    public static unsafe int GetString32(string s)
    {
        fixed (char* str = s)
        {
            char* chPtr = str;
            int num = 0x15051505;
            int num2 = num;
            int* numPtr = (int*)chPtr;
            for (int i = s.Length; i > 0; i -= 4)
            {
                num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0];
                if (i <= 2)
                {
                    break;
                }
                num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1];
                numPtr += 2;
            }
            return (num + (num2 * 0x5d588b65));
        }
    }


    public static unsafe int GetString64(string s)
    {
        fixed (char* str = s)
        {
            int num3;
            char* chPtr = str;
            int num = 0x1505;
            int num2 = num;
            for (char* chPtr2 = chPtr; (num3 = chPtr2[0]) != '\0'; chPtr2 += 2)
            {
                num = ((num << 5) + num) ^ num3;
                num3 = chPtr2[1];
                if (num3 == 0)
                {
                    break;
                }
                num2 = ((num2 << 5) + num2) ^ num3;
            }
            return (num + (num2 * 0x5d588b65));
        }
    }
}
0

Source: https://habr.com/ru/post/1546785/


All Articles