none
WCF ServiceClient(System.ServiceModel.ClientBase) send Custom Authorization Token RRS feed

  • Question

  • Hi I have a wcf client and use   ServiceClient(System.ServiceModel.ClientBase) and I want to add custom authorization token to my request

    In HttpClient we can do it easily,

                 HttpClient restClient = new System.Net.Http.HttpClient();

                restClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                restClient.BaseAddress = new Uri(BaseAddress);


    How can I do it wcf  soap ServiceClient call?like sclient.Headers.Add(....) ??

      ServiceReference1.Service1Client sclient = new ServiceReference1.Service1Client("WSHttpBinding_IService1");

    //TODO something to add Authorization header

           var x = sclient.GetData("1");

       
    Wednesday, December 27, 2017 7:30 AM

Answers

  • Hi Bilgehan,

    >> I want to add custom authorization token to my request

    How will you validate the header at service side?

    For adding http header, you could try code below:

                WCFRestSvcClient.Service1Client client = new WCFRestSvcClient.Service1Client();
                using (new OperationContextScope(client.InnerChannel))
                {
                    // Add a HTTP Header to an outgoing request
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    requestMessage.Headers["Authorization"] = "ABCD";
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                    string result = client.GetData(123);
                }

    In addition, has issue below resolved? If it has, I would suggest you mark the helpful reply as answer to close it.

    # WCF TOKEN BASED SECURITY TYPE FR TRANSPORT LEVEL

    https://social.msdn.microsoft.com/Forums/en-US/e7ebf8ef-423e-4e98-b5c2-5079a9d2583f/wcf-token-based-security-type-fr-transport-level?forum=wcf

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 28, 2017 3:16 AM

All replies

  • Hi Bilgehan,

    >> I want to add custom authorization token to my request

    How will you validate the header at service side?

    For adding http header, you could try code below:

                WCFRestSvcClient.Service1Client client = new WCFRestSvcClient.Service1Client();
                using (new OperationContextScope(client.InnerChannel))
                {
                    // Add a HTTP Header to an outgoing request
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    requestMessage.Headers["Authorization"] = "ABCD";
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                    string result = client.GetData(123);
                }

    In addition, has issue below resolved? If it has, I would suggest you mark the helpful reply as answer to close it.

    # WCF TOKEN BASED SECURITY TYPE FR TRANSPORT LEVEL

    https://social.msdn.microsoft.com/Forums/en-US/e7ebf8ef-423e-4e98-b5c2-5079a9d2583f/wcf-token-based-security-type-fr-transport-level?forum=wcf

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 28, 2017 3:16 AM
  • Thank for your answer,Yes I will validate header at server side
    Thursday, December 28, 2017 6:07 AM