加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-8 – 如何将自定义授权标头添加到Windows应用商店OData

发布时间:2020-12-13 22:29:55 所属栏目:Windows 来源:网络整理
导读:我正在使用 Windows运行时构建 Windows应用商店应用.我正在访问使用基本身份验证的OData服务.我正在使用 WCF Data Services Tools for Windows Store Apps库(Microsoft.Data.Services.Client.WindowsStore). 身份验证字符串是自定义格式,因此我不能只使用Net
我正在使用 Windows运行时构建 Windows应用商店应用.我正在访问使用基本身份验证的OData服务.我正在使用 WCF Data Services Tools for Windows Store Apps库(Microsoft.Data.Services.Client.WindowsStore).

身份验证字符串是自定义格式,因此我不能只使用NetworkCredential(用户名,密码).我需要自己添加标头到我的DataServiceContext的每个请求.

我尝试使用以下代码:

proxy.SendingRequest += (s,e) =>
{
   e.RequestHeaders.Add("Authorization",authHeader);
}

但我收到错误:

'System.Net.WebHeaderCollection' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Net.WebHeaderCollection' could be found

解决方法

您可以使用在构建请求之后以及将其发送到服务器之前触发的新 SendingRequest2事件.

有一个RequestMessage.SetHeader(headername,value)方法可用于设置标题.将值设置为null以删除标头.

proxy.SendingRequest2 += (sender,eventArgs) =>
{
    eventArgs.RequestMessage.SetHeader("Authorization",authHeader);
};

WCF Data Services team blog更多地谈论它:

SendingRequest2 (and its deprecated predecessor SendingRequest) fires after the request is built. WebRequest does not allow you to modify the URL after construction. The new event lets you modify the URL before we build the underlying request,giving you full control over the request.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读