Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.ServiceModel;
- using System.ServiceModel.Description;
- using System.ServiceModel.Dispatcher;
- using System.ServiceModel.Channels;
- namespace SiebelCrmConnector
- {
- /// <summary>
- /// Message inspector that holds session identification and applies it to outgoing messages
- /// </summary>
- public class SessionHandlingInspector : IEndpointBehavior, IClientMessageInspector
- {
- /// <summary>
- /// Creates a new SessionHandlingInspector that will use given <paramref name="cookie"/> for authentication
- /// </summary>
- /// <param name="cookie">The Set-Cookie header containing the session id</param>
- public SessionHandlingInspector(string cookie)
- {
- this.Cookie = cookie;
- }
- /// <summary>
- /// Stores the Set-Cookie header for the session
- /// </summary>
- private string Cookie { get; set; }
- #region IEndpointBehavior Members
- public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
- {
- }
- /// <summary>
- /// Triggered when inspector is added to an endpoint. Registeres the instance as message inspector
- /// </summary>
- /// <param name="endpoint"></param>
- /// <param name="clientRuntime"></param>
- public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
- {
- // adds our inspector to the runtime
- clientRuntime.MessageInspectors.Add(this);
- }
- public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
- {
- }
- public void Validate(ServiceEndpoint endpoint)
- {
- }
- #endregion
- #region IClientMessageInspector Members
- void IClientMessageInspector.AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
- {
- }
- /// <summary>
- /// Triggered before sending a request. Adds the cookie header to the outgoing message
- /// </summary>
- /// <param name="request"></param>
- /// <param name="channel"></param>
- /// <returns></returns>
- object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
- {
- HttpRequestMessageProperty httpRequest;
- if (!request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
- {
- request.Properties.Add(HttpRequestMessageProperty.Name,
- new HttpRequestMessageProperty());
- }
- httpRequest = (HttpRequestMessageProperty)
- request.Properties[HttpRequestMessageProperty.Name];
- httpRequest.Headers.Add(HttpRequestHeader.Cookie, this.Cookie);
- return null;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment