我有一个运行在Azure Web Roles中的网站。我在asafaweb.com网站上测试了这个网站,并得到了“过多的标题”警告。Azure webrole excessive headers
基本上天青送出IIS版本和.NET版本作为报头的一部分。
有很多关于如何在IIS中关闭这些标头的信息,但是如何在Azure中关闭它们?
我有一个运行在Azure Web Roles中的网站。我在asafaweb.com网站上测试了这个网站,并得到了“过多的标题”警告。Azure webrole excessive headers
基本上天青送出IIS版本和.NET版本作为报头的一部分。
有很多关于如何在IIS中关闭这些标头的信息,但是如何在Azure中关闭它们?
这是我在大多数项目中使用隐藏这些标题:
的Global.asax.cs(仅适用于MVC项目)
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
}
自定义的HttpModule
public class RemoveHeadersHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
private void OnPreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Remove("Server");
HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
}
public void Dispose()
{
}
}
的web.config
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="Server" />
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true">
. . .
<add name="RemoveHeadersHttpModule" type="MyNamespace.RemoveHeadersHttpModule"/>
</modules>
. . .
</system.webServer>
Windows Azure Web角色本质上是Windows Server 2008,启用了IIS。所以,如果你想定制IIS,你可以使用一个启动脚本并调用appcmd来改变你想要的设置(或者用你通常做的其他方式来操作)。您的脚本看起来像:
%windir%\system32\inetsrv\appcmd set ...
如果你想有一个完整的解决方案,以消除天青也与卡西尼工作没有使用自定义的HttpModule所有过多的头,在这里看到:
Removing/Hiding/Disabling excessive HTTP response headers in Azure/IIS7 without UrlScan
我还没有真正尝试过。我相信它会起作用,但是@sandrino Di Mattia的回答要简单得多 – Greg 2012-08-20 06:45:14