Programatically removing etag suffix (change number) from the metabase IIS6

霸气de小男生 提交于 2019-12-10 20:16:25

问题


IIS 6.0 generates eTag values in the format of "hash:changenumber". The changenumber goes up every time IIS resets, so your eTag is only valid for the lifetime of your IIS process. Restart, number goes up, hash:changenumber != hash:changenumber+1.

The fix for this is to hard-code the changenumber, which is possible using the Metabase Explorer, a .NET utility for editing the metabase, or by editing the XML file when the IIS services are stopped.

I want to do this programmatically, with the server running, like I can set all the other metabase properties with either ADSI or WMI. For this one it doesn't seem to be possible, as the property (which is only internally referred to as MD_ETAG_CHANGENUMBER) does not appear to have a matching property name.

Here's an example of the problem in VBScript:

set obj=GetObject("IIS://localhost/W3svc")
WScript.Echo "Log type: " & obj.LogType
WScript.Echo "Change number: " & obj.MD_ETAG_CHANGENUMBER

The output:

Log type: 1
etag.vbs(3, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'obj.MD_ETAG_CHANGENUMBER'

I want to be able to set this value in C#. Short of stopping IIS, setting the value in the XML, and starting it again, is there a method of setting this value programatically?

My best thought is (ab)using the IISMbLib.dll that comes with Metabase Explorer, so if anyone has experience using this, I'd love to hear it.

References:

  • MD_ETAG_CHANGENUMBER Metabase Property (IIS 6.0)
  • IIS forums thread discussing the problem, with no resolution

回答1:


crb, thanks for the great solution, I was unable to find an alternative (although I have previously added my own custom metabase properties to the IIS 6 schema through some tricky ADSI scripting, that are used by a custom ISAPI)

Here is a powershell version of your solution. It assumes MB Explorer assembly copied locally to it.

$myPath = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)

Import-Module "$myPath\IISMbLib.dll"

$etagValue = 12345
$metabase = New-Object IISConfig.Metabase
$metabase.OpenLocalMachine()

$key = $metabase.GetKeyFromPath("/LM/W3SVC")

if ($key.ContainsRecord(2039) -eq [IISConfig.ValueExistOptions]::Explicit)
{
    $record = $key.GetRecord(2039)
    Write-Host "Existing ETag value found:", $record.Data.ToString()
}
else
{
    Write-Host "Creating new value..."
    $record = New-Object IISConfig.Record
    $record.DataType = [IISConfig.Record+DataTypes]::DWORD
    $record.Identifier = 2039
    $record.ChangeAttribute([IISConfig.Record+AttributeList]::Inherit, $true)
}
$record.Data = [System.Convert]::ToUInt32($etagValue)
Write-Host "New ETag value:", $record.Data.ToString()
$key.SetRecord($record)



回答2:


My best thought was pretty good. Here is a solution, which depends on IISMbLib.dll from the Metabase Explorer in the IIS 6.0 Resource Kit.

        Metabase metabase = new Metabase();
        metabase.OpenLocalMachine();

        IKey key = metabase.GetKeyFromPath("/LM/W3SVC/");
        if (key.ContainsRecord(2039) == IISConfig.ValueExistOptions.Explicit) {
            Record r = key.GetRecord(2039);
            r.Data = Convert.ToUInt32(0);
            key.SetRecord(r);
        } else {
            Record r = new Record();
            r.Data = Convert.ToUInt32(0);
            r.DataType = Record.DataTypes.DWORD;
            r.Identifier = 2039;
            r.ChangeAttribute(Record.AttributeList.Inherit, true);
            key.SetRecord(r);
        }


来源:https://stackoverflow.com/questions/922472/programatically-removing-etag-suffix-change-number-from-the-metabase-iis6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!