locked
Custom EndpointBehaviors In WinRT RRS feed

  • Question

  • Hello, I'm trying to port a WinForms app to Win RT / Metro, and am trying to modify the http request's message properties to add the Authorization header to the outgoing message for my WCF Service.  I'm doing this because I need to pre-authenticate against a service that's using basic authentication on IIS.  In WinForms I did this:

    mProxy.ChannelFactory.Endpoint.EndpointBehaviors.Add(new CustomAuthenticationEndpointBehavior());

    Then I defined my custom endpoint behavior as:

      /// <summary>
            /// This class will override the message properties in the header for each message to avoid any 401 unauthorized messages.
            /// </summary>
            /// <remarks></remarks>
            public class CustomAuthenticationEndpointBehavior : System.ServiceModel.Description.IEndpointBehavior
            {
                
                public void AddBindingParameters(System.ServiceModel.Description.ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
                {
                    return;
                }
                public void ApplyClientBehavior(System.ServiceModel.Description.ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
                {
                    clientRuntime.ClientMessageInspectors.Add(new CustomAuthenticationMessageInspector());
                }
                public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
                {
                    return;
                }
                public void Validate(System.ServiceModel.Description.ServiceEndpoint endpoint)
                {
                    return;
                }
            }
            public class CustomAuthenticationMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector
            {
                public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
                {
                }
                /// <summary>
                /// Before each message request, add the basic authentication HTTP message header (NOT SOAP HEADER) - this always including the authorization header, eliminating any 401 messaegs.
                /// </summary>
                /// <param name="request"></param>
                /// <param name="channel"></param>
                /// <returns></returns>
                /// <remarks></remarks>
                public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
                {
                    //Get the request message property header
                    System.ServiceModel.Channels.HttpRequestMessageProperty rmp = default(System.ServiceModel.Channels.HttpRequestMessageProperty);
                    //Check if the httpRequestProperty exists
                    if (request.Properties.ContainsKey(System.ServiceModel.Channels.HttpRequestMessageProperty.Name) == true)
                    {
                        //Get the request message property header
                        rmp = (System.ServiceModel.Channels.HttpRequestMessageProperty) request.Properties[System.ServiceModel.Channels.HttpRequestMessageProperty.Name];
                    }
                    else
                    {
                        //Create a new request property
                        rmp = new System.ServiceModel.Channels.HttpRequestMessageProperty();
                        request.Properties.Add(System.ServiceModel.Channels.HttpRequestMessageProperty.Name, rmp);
                    }
                   
                    rmp.Headers.Add("Authorization: Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("UserName" + ":" + "Password")));
                    return null;
                }
            }

    This all works until you get to the rmp.Headers.Add method - for some reason the System.ServiceModel.Channels.HttpRequestMessageProperty.Add method is not included in WinRT.  I know of another way to get to get this to work without the custom endpoint behavior but I was hoping someone knew how to add to the headers collection.

    Thanks.

    Saturday, August 11, 2012 5:58 PM

Answers

  • Hi Ryan_Ha,

    The Add method is not exposed in WinRT. To add the headers in the collection you can use the following syntax:

    rmp.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("UserName" + ":" + "Password")));

    Thanks,

    Prashant.

    • Marked as answer by Ryan_Ha Tuesday, August 14, 2012 12:53 AM
    Tuesday, August 14, 2012 12:01 AM
    Moderator

All replies

  • Hi Ryan_Ha,

    The Add method is not exposed in WinRT. To add the headers in the collection you can use the following syntax:

    rmp.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("UserName" + ":" + "Password")));

    Thanks,

    Prashant.

    • Marked as answer by Ryan_Ha Tuesday, August 14, 2012 12:53 AM
    Tuesday, August 14, 2012 12:01 AM
    Moderator
  • Thanks Prashant, I didn't know you could do it that way, thanks so much!
    Tuesday, August 14, 2012 12:53 AM