如何删除不需要的WWW-Authenticate标头

时间:2012-10-23 04:20:17

标签: asp.net-mvc-3 iis-7 icalendar plesk

在MVC应用程序中,我正在根据此SO问题的答案采购带有身份验证的iCal订阅:

Serving an iCalendar file in ASPNET MVC with authentication

使用DDay.iCal库从数据库中的事件动态创建iCal流。

此解决方案在本地开发服务器上运行良好:OSX日历和Outlook都可以订阅并从应用程序接收更新。

但是,在我的Web主机的共享服务器上,Calendar和Outlook的身份验证都失败了。也就是说,他们都一直在问我用户和用户。 (正确)失败后的密码。

编辑:如果我将浏览器指向日历网址,它也无法通过身份验证。

编辑:获取weirder-Firefox进行身份验证并获取iCal文件。 Safari,Chrome和IE验证失败。

如果我使用相同的凭据将curl指向日历URL,我就成功了(即我得到了所需的iCal文件)。当然,可以使用相同的凭据登录MVC应用程序。

编辑 - 我想我知道发生了什么,但我不知道如何修复它。在我的OnAuthorization()中,我只添加了WWW-Authentication Basic但是在Fiddler中我可以看到提供了三种类型的身份验证:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Secure Calendar"
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
... etc ...

此时只有Firefox使用基本授权进行响应,后者成功。

GET <<URL>> HTTP/1.1
...
Authorization: Basic <<encoded credentials>>

IE以Negotiate响应,但失败

GET <<URL>> HTTP/1.1
...
Authorization Negotiate <<encoded stuff>>

谁在添加其他两个,我该如何让它停止?以下是服务器响应的更多详细信息:

HTTP/1.1 401 Unauthorized
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
WWW-Authenticate: Basic realm="Secure Calendar"
X-AspNet-Version: 4.0.30319
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Tue, 23 Oct 2012 13:27:48 GMT

谢谢, 埃里克

5 个答案:

答案 0 :(得分:4)

哈哈,答案在于IIS的配置。

我要求主持人的管理员关闭其他身份验证,除了iCal Feed之外,其他所有身份验证都已破坏。

现在他们又重新开启了一对,MVC网站的工作方式与日历提要一样有效......哇!非常,非常大的笑容。

以下是我们最终的IIS配置:

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

我不确定为什么这个有用 - 或者还有什么可能会破坏 - 但今天我很高兴。

答案 1 :(得分:3)

WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM

由Windows身份验证使用。由于您最终启用了匿名身份验证,因此不会显示所有WWW-Authenticate标头。

答案 2 :(得分:1)

简单方法:

如果您希望从每个新创建的域中删除此“ X-Powered-By-Plesk ”标题,则可以在其中创建默认的 web.config 文件“默认主机模板”的“httpdocs”文件夹。

此默认网站模板通常位于:“ C:\ inetpub \ vhosts.skel \ 0 \ httpdocs ”。 创建新网站时,默认情况下将使用 web.config 文件。

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

提示1:您可以使用此方法删除任何不需要的自定义标题(为了不向坏人讲述您的服务器太多):

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

提示2:如果您要删除任何动态标头(例如着名的“服务器”标头),您需要使用 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>

提示3:此外,您可以使用此默认 web.config 文件来设置要用于每个新网站的所有配置参数(例如:为您的网站定义默认文档列表网站,如本Plesk帮助文章中所述:https://support.plesk.com/hc/en-us/articles/213364049-How-to-configure-global-default-document-settings-in-Parallels-Plesk

答案 3 :(得分:0)

作为对此的迟来答复,您还可以通过创建自定义消息处理程序来解决此问题。

消息处理程序将继承自DelegatingHandler,必须将其添加到HttpConfiguration的{​​{1}}

如下所示:

MessageHandlers

然后将其注册到HttpConfiguration中,如下所示:

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;
    }
}

您可能会从全局配置中调用。消息处理程序也可以直接附加到路由,因此,如果您不希望它随处可见,只需查看MSDN上的linked article以获得更多说明

答案 4 :(得分:0)

我有同样的问题。

响应包括3个WWW-Authenticate标头,只有Firefox可以正常工作。 Chrome,Bing和IE提示输入用户名和密码,但之后他们没有将身份验证标头发送到服务器。

我刚刚更改了IIS身份验证设置,此问题已解决:

Anonymous Authentication  Enabled
ASP.NET Impersonation     Disabled
Basic Authentication      Disabled          HTTP 401 Challenge
Forms Authentication      Disabled          HTTP 302 Login/Redirect
Windows Authentication    Disabled          HTTP 401 Challenge
相关问题