你擔任這個beeing IIS 6的問題可能是正確的。
我剛剛創建了一個WCF服務「xxx.svc」和託管它的IIS(7.5),當我用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([email protected]"/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);
}
}
}
個
提琴手結果:
你想要的方式在你的WCF REST Web服務方法讀取HTTP標頭? – CSharpenter 2012-03-05 13:57:37