通过Fidder进行Restful WCF和基本身份验证

时间:2012-03-05 12:54:13

标签: wcf authentication rest fiddler basic-authentication

我已经创建了一个WCF休息Web服务。但是,客户端已请求使用基本身份验证锁定服务,但允许他们在第一次响应而不是质询时提供授权令牌。不幸的是,我的测试机上只有IIS 6

我只需要模拟基本身份验证,因此我通过匿名进行此操作,如果授权令牌不正确则抛出错误。但是,身份验证令牌不可用于WCF

http://localhost/test.svc/get/token/

内容类型:application / x-www-form-urlencoded

授权:基本Base64Value

如果我删除匿名并在IIS中添加basic。我得到的只是401.我猜在IIS中进行WCF之前的身份验证。

理想情况下,我只是喜欢无聊的访问权限,并且能够访问授权标题。

如何获取auth标头

1 个答案:

答案 0 :(得分:1)

您对此问题的假设可能是正确的。

我刚刚创建了一个WCF服务“xxx.svc”并将其托管在IIS(7.5)中,当我使用带有正确Authorization标头的fiddler2请求它时,它没有发送HTTP 401。

我将发布我的代码,以便您在IIS 6上进行测试。如果它仍然提供HTTP 401,那么这肯定是一个IIS 6问题,如果不是尝试比较和对比我的代码,看看有什么配置不同。

的web.config:

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindConfig">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" proxyCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="MyTestSvc.MyService">
        <endpoint address="http://localhost/TestBasicAuth/Service1.svc" behaviorConfiguration="webHttpEndpointBehavior"
          binding="webHttpBinding" bindingConfiguration="webHttpBindConfig"
          name="webHttpBindingEndpoint" contract="MyTestSvc.IMyService" />
        <host>
          <baseAddresses>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpEndpointBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Service1.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyTestSvc.MyService" CodeBehind="Service1.svc.cs" %>

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyTestSvc
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IMyService
    {

        [OperationContract]
        [WebGet(UriTemplate=@"/Hello")]
        string GetData();

    }
}

最后:Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyTestSvc
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class MyService : IMyService
    {
        public string GetData()
        {
            WebOperationContext webCtx = WebOperationContext.Current; 
            IncomingWebRequestContext incomingCtx = webCtx.IncomingRequest; 
            string hdrVal = incomingCtx.Headers["Authorization"]; 

            return string.Format("Authorization: {0}", hdrVal);
        }
    }
}

fiddler结果: Fiddler Composer request

fiddler inspector, request/response raw