首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restful WCF和通过Fidder的基本身份验证

Restful WCF和通过Fidder的基本身份验证
EN

Stack Overflow用户
提问于 2012-03-05 20:54:13
回答 1查看 3K关注 0票数 1

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

我只需要模拟基本身份验证,所以我在匿名上做了这件事,如果授权令牌不正确,就会抛出一个错误。但是,身份验证令牌对WCF不可用

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

Content-Type: application/x-www-form-urlencoded

授权:基本Base64Value

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

理想情况下,我只是喜欢无源地访问,并且能够访问authorization头。

如何获取auth头部

EN

回答 1

Stack Overflow用户

发布于 2012-03-05 21:25:17

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

我刚刚创建了一个WCF服务"xxx.svc“并将其托管在IIS7.5中,当我使用带有正确的Authorization的fiddler2请求它时,它没有发送HTTP401。

我会发布我的代码,这样你就可以在IIS 6上测试它了。如果它仍然给你HTTP 401,那么这肯定是IIS 6的问题,如果不是的话,试着把你的代码和我的代码进行比较,看看有什么配置不同。

web.config:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<%@ ServiceHost Language="C#" Debug="true" Service="MyTestSvc.MyService" CodeBehind="Service1.svc.cs" %>

IService1.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9566986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档