using System; using System.Collections.Generic; using System.Reflection; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Dispatcher; using System.ServiceModel.Description; namespace TGS.Interface { /// /// Used to attach windows credential headers to SOAP messages /// sealed class AuthenticationHeaderApplicator : IEndpointBehavior, IClientMessageInspector { /// /// The credentials to attach /// readonly RemoteLoginInfo remoteLoginInfo; /// /// Construct a /// /// The to use public AuthenticationHeaderApplicator(RemoteLoginInfo loginInfo) { remoteLoginInfo = loginInfo; } /// /// Add to the message inspectors for the channel /// /// The /// The public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { var t = Type.GetType("Mono.Runtime"); ICollection inspectors; if (t != null) { var prop = clientRuntime.GetType() .GetTypeInfo() .GetDeclaredProperty("MessageInspectors"); inspectors = (ICollection)prop .GetValue(clientRuntime); } else inspectors = clientRuntime.ClientMessageInspectors; inspectors.Add(this); } /// /// Attach and to the /// /// The outgoing request /// The /// public object BeforeSendRequest(ref Message request, IClientChannel channel) { request.Headers.Add(MessageHeader.CreateHeader("Username", "http://tempuri.org", remoteLoginInfo.Username)); request.Headers.Add(MessageHeader.CreateHeader("Password", "http://tempuri.org", remoteLoginInfo.Password)); return null; } /// /// Unused implementation of /// public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { //intentionally left blank } /// /// Unused implementation of /// public void AfterReceiveReply(ref Message reply, object correlationState) { //intentionally left blank } /// /// Unused implementation of /// public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { //intentionally left blank } /// /// Unused implementation of /// public void Validate(ServiceEndpoint endpoint) { //intentionally left blank } } }