2012-08-01 67 views
0

我有一個運行在Azure Web Roles中的網站。我在asafaweb.com網站上測試了這個網站,並得到了「過多的標題」警告。Azure webrole excessive headers

asafaweb.com screenshot

基本上天青送出IIS版本和.NET版本作爲報頭的一部分。

有很多關於如何在IIS中關閉這些標頭的信息,但是如何在Azure中關閉它們?

回答

3

這是我在大多數項目中使用隱藏這些標題:

的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> 
1

Windows Azure Web角色本質上是Windows Server 2008,啓用了IIS。所以,如果你想定製IIS,你可以使用一個啓動腳本並調用appcmd來改變你想要的設置(或者用你通常做的其他方式來操作)。您的腳本看起來像:

%windir%\system32\inetsrv\appcmd set ...

+0

我還沒有真正嘗試過。我相信它會起作用,但是@sandrino Di Mattia的回答要簡單得多 – Greg 2012-08-20 06:45:14