Azure移动服务:如何将自定义标头注入请求中?

时间:2015-11-05 14:03:27

标签: c# azure xamarin.ios azure-mobile-services

我在Xamarin.iOS应用中使用Azure移动服务。 我的服务期望从客户端发送自定义标头。

为了发送它,我创建了自己的消息处理程序,我从`NativeMessageHandler(ModernHttpClient的一部分)派生出来:

this.client = new MobileServiceClient (Constants.ApplicationURL, Constants.GatewayURL, new CustomMessageHandler ());

要获得我的标题,处理程序如下所示:

public class CustomMessageHandler: NativeMessageHandler
{
        protected override Task<System.Net.Http.HttpResponseMessage> SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
  {
            request.Headers.Add ("customHeader", "headerValue");
            return base.SendAsync (request, cancellationToken);
  }
}

问题是,当我的服务部署到Azure时,标头似乎没有到达服务器。 在VisualStudio 中本地运行时可以正常工作。如果我在我的处理程序中放置一个断点,它就会被击中;所以标题真的被添加了。

为什么它会在本地工作但不在Azure上工作?

1 个答案:

答案 0 :(得分:1)

答案是: D'oh!

我为Azure应用启用了“身份验证”。因此,我会为每个请求获得401。它与我的处理程序无关。把它关掉,就像魅力一样。