我做了一个小项目(WCF + REST),但遇到了一个小问题。我想制作我的授权和身份验证类。
我的授权类:
//validate api key
public class BasicAuthorization : ServiceAuthorizationManager
{
public override bool CheckAccess(OperationContext operationContext,
ref Message message)
{
//some code
}
}
我的身份验证类
// validation user login & password
public class BasicAuthentication : ServiceAuthenticationManager
{
public override ReadOnlyCollection<IAuthorizationPolicy> Authenticate(
ReadOnlyCollection<IAuthorizationPolicy> authPolicy, Uri listenUri,
ref Message message)
{
//some code
}
}
我有太多的配置文件
<behavior>
<serviceAuthorization
serviceAuthorizationManagerType="WCF.BasicAuthorization, WCF"/>
<serviceAuthenticationManager
serviceAuthenticationManagerType="WCF.BasicAuthentication, WCF"/>
</behavior>
类中的代码不重要 - 不是问题。
我的问题是如何从 operationContext 或消息类中获取 header 。我之前是怎么说的,我让它处于静止状态,所以我想要手动设置 Authorizaion header /www-authenticate header ,但应用程序看不到它。
我打开 Fiddler2,并尝试放置任何 header ,例如:
Content-Type: application/xml
Authorization: Basic bla23rwerfsd3==
User-Agent: Fiddler
Host: localhost:59305
并且 message.Headers/operationContext.Headers 没有任何我的 header (只有其他 header ),没有授权,没有内容类型
最佳答案
您可以在 Web 操作期间使用 System.ServiceModel.Web.WebOperationContext 类访问 header ,该类具有静态属性“Current”,表示当前上下文。它提供了一个“IncomingRequest”属性,其中包含一个“WebHeaderCollection”类型的“Header”属性。
关于c# - OperationContext 中的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5117999/