OperationContext中的标头

use*_*199 5 c# wcf

我做了一个小项目(WCF + REST),我遇到了一个小问题.我想创建我的授权和身份验证类.

我的授权类:

//validate api key
public class BasicAuthorization : ServiceAuthorizationManager
{
    public override bool CheckAccess(OperationContext operationContext, 
        ref Message message)
    {
        //some code
    }
}
Run Code Online (Sandbox Code Playgroud)

我的认证课程

// validation user login & password
public class BasicAuthentication : ServiceAuthenticationManager
{
    public override ReadOnlyCollection<IAuthorizationPolicy> Authenticate(
        ReadOnlyCollection<IAuthorizationPolicy> authPolicy, Uri listenUri, 
        ref Message message)
    {
        //some code
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一些配置文件

<behavior>
  <serviceAuthorization 
      serviceAuthorizationManagerType="WCF.BasicAuthorization, WCF"/>
  <serviceAuthenticationManager 
      serviceAuthenticationManagerType="WCF.BasicAuthentication, WCF"/>
</behavior>
Run Code Online (Sandbox Code Playgroud)

类中的代码不重要 - 不是问题.

我的问题是如何从operationContext或消息类中获取Headers.我之前怎么说,我让它休息,所以我想手动设置Authorizaion标题/ www-authenticate标题,但应用程序没有看到它.

我打开Fiddler2,尝试放任何标题例如:

Content-Type: application/xml
Authorization: Basic bla23rwerfsd3==
User-Agent: Fiddler
Host: localhost:59305
Run Code Online (Sandbox Code Playgroud)

而message.Headers/operationContext.Headers没有任何我的标题(只有其他标题),没有授权,没有内容类型

J. *_*hon 14

您可以使用System.ServiceModel.Web.WebOperationContext类在Web操作期间访问标头,该类具有静态属性"Current",表示当前上下文.它提供了一个"IncomingRequest"属性,其中包含"WebHeaderCollection"类型的"Header"属性.