How can I add authorization header to the request in WCF?

前端 未结 4 1513
后悔当初
后悔当初 2020-12-10 17:03

I\'m working on a Windows Form application and there\'s a WCF service that needs to be called. I need to add a header (authorization - custom) to the request before it\'s se

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 17:22

    The simplest way is to add it on the client side:

    using (MyServ.ServiceClient proxy = new MyServ.ServiceClient())
    {
         using (new System.ServiceModel.OperationContextScope(proxy.InnerChannel))
         {
             MessageHeader head = MessageHeader.CreateHeader("Authorization", "http://yournamespace.com/v1", data);
             OperationContext.Current.OutgoingMessageHeaders.Add(head);
         }
    }
    

    and retrieve it on the server side:

    string  auth = OperationContext.Current.IncomingMessageHeaders.
    GetHeader("Authorization", "http://mynamespace.com/v1");
    

    I also suggest that you check these articles:

    Authorization Header is missing in Http request using WCF

    WCF Service with wsHttpBinding - Manipulating HTTP request headers

提交回复
热议问题