我正在尝试将API密钥附加到OperationContext出站消息头,如下所示:
public static void AddApikeyToHeader(string apikey, IContextChannel channel, string address)
{
using (OperationContextScope scope = new OperationContextScope(channel))
{
MessageHeader header = MessageHeader.CreateHeader("apikey", address, apikey);
OperationContext.Current.OutgoingMessageHeaders.Add(header);
}
}
但是我不知道如何在服务器端检索标头。我使用服务授权管理器并获取当前操作上下文,尝试像这样检索标头:
public string GetApiKey(OperationContext operationContext)
{
var request = operationContext.RequestContext.RequestMessage;
var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
return prop.Headers["apikey"];
}
但是没有附加apikey头。此外,在调试时,当我检查operationContext时,似乎无法看到我的apikey头。有人能看出我错在哪里吗?
IMyServiceChannel
的定义在哪里? - Chris Pickford