How to remove unwanted WWW-Authenticate headers

南楼画角 提交于 2019-12-05 14:12:30

Ha ha, the answer lay in IIS configuration.

I asked the admins at my host to turn off the other authentications, which broke everything but the iCal feed.

Now they've turned a couple back on again and the MVC site works as well as the calendar feed with authentication... whew! Very, very big smile.

Here's the IIS configuration we ended up with:

Name                        Status         Response Type
Anonymous Authentication    Enabled
ASP.NET Impersonation       Disabled
Basic Authentication        Disabled       HTTP 401 Challenge
Digest Authentication       Disabled       HTTP 401 Challenge
Forms Authentication        Enabled        HTTP 302 Login/Redirect
Windows Authentication      Enabled        HTTP 401 Challenge

I'm not sure why this works—or what else might break—but today I'm happy.

WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM

are used by Windows authentication. Since you finally enabled anonymous authentication, all WWW-Authenticate headers will not appear.

Easy way :

If you want this "X-Powered-By-Plesk" Header to be removed from EVERY NEWLY created domains, you can create a default web.config file within the "httpdocs" folder of the "Default Host Template".

This default website template is usually located under : "C:\inetpub\vhosts.skel\0\httpdocs". That web.config file will be used by default when you create a new website.

<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <remove name="X-Powered-By-Plesk" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </configuration>

TIP 1 : You can use this method to remove any unwanted Custom header (In order to not tell too much to bad guys about your server) :

<remove name="X-Powered-By"/>
<remove name="X-Powered-By-Plesk"/>
<remove name="X-AspNet-Version"/>
<remove name="X-AspNetMvc-Version"/>

TIP 2 : If you want to remove any Dynamic header (like the famous "Server" header), you will need to operate with outboundRules :

  <configuration>
    <system.webServer>
      <rewrite>
        <outboundRules>
          <rule name="StripHeader_Server" patternSyntax="Wildcard">
            <match serverVariable="RESPONSE_SERVER" pattern="*"/>
            <action type="Rewrite" value=""></action>
          </rule>
          <rule name="StripHeader_ETag">
            <match serverVariable="RESPONSE_ETag" pattern=".+" />
            <action type="Rewrite" value="" />
          </rule>
        </outboundRules>
      </rewrite>
    </system.webServer>
  </configuration>

TIP 3 : Additionally, you can use this default web.config file to set all configuration parameters you want to use for every new website (in example : to define a list of default documents for your websites, as explained on this Plesk Help article : https://support.plesk.com/hc/en-us/articles/213364049-How-to-configure-global-default-document-settings-in-Parallels-Plesk )

As a belated answer to this, you could also handle this by creating a custom message handler.

The message handler would be inheriting from DelegatingHandler and has to be added to the HttpConfiguration its MessageHandlers

A way this could look would be the following:

public class EnsureNoAuthenticationHeaderHandler : DelegatingHandler 
{
    async protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken ) 
    {
        var response = await base.SendAsync( request, cancellationToken );
        if ( response.StatusCode == System.Net.HttpStatusCode.Unauthorized ) 
        {
            response.Headers.Remove( "WWW-Authenticate" );
        }
        return response;
    }
}

And then register it in the HttpConfiguration somewhat like the following

private void Register( HttpConfiguration configuration ) 
{
    configuration.MessageHandlers.Add( new EnsureNoAuthenticationHeaderHandler() );
}

Which you would probably call from your global configuration. A message handler can also be attached to a route directly, so if you don't want it to be available everywhere, just have a looked at the linked article on MSDN for more explanation

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