Win2k8:如何在 IIS 中设置“拒绝文件扩展名”规则?

Win2k8:如何在 IIS 中设置“拒绝文件扩展名”规则?

有谁知道如何使用 Powershell 创建“拒绝文件扩展名”规则,以便某些文件不会由 IIS 提供?

例如,我想设置一条规则来拒绝所有 .txt 文件通过 IIS 提供。

谢谢

答案1

这个怎么样:

[Reflection.Assembly]::Load(
"Microsoft.Web.Administration, Version=7.0.0.0, 
Culture=Neutral, PublicKeyToken=31bf3856ad364e35") > $null

$serverManager = New-Object Microsoft.Web.Administration.ServerManager

# Default Web Site
$siteId = 1
$site = $serverManager.Sites | where { $_.Id -eq $siteID }   

#
# Sets the filter in the site's web.config
#
$config = $site.GetWebConfiguration()
$requestFilteringSection = `
      $config.GetSection("system.webServer/security/requestFiltering")

#
# Or delete last two lines and use next two instead to set in applicationHost.config
#
# $config = serverManager.GetApplicationHostConfiguration();
# $requestFilteringSection = `
    $config.GetSection("system.webServer/security/requestFiltering", $site.Name);

$fileExtensionsElement = `
      $requestFilteringSection.GetChildElement("fileExtensions")
$fileExtensionsCollection = $fileExtensionsElement.GetCollection()
$addElement = $fileExtensionsCollection.CreateElement("add")
$addElement["fileExtension"] = ".txt"
$addElement["allowed"] = $false
$fileExtensionsCollection.Add($addElement)

$serverManager.CommitChanges()

您还可以使用appcmd.exe

在站点配置web.config

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='.txt',allowed='False']" 

或者配置为applicationHost.config

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='.bong',allowed='False']" /commit:apphost

相关内容