Guest User

SessionHandlingInspector

a guest
Nov 28th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.ServiceModel;
  7. using System.ServiceModel.Description;
  8. using System.ServiceModel.Dispatcher;
  9. using System.ServiceModel.Channels;
  10.  
  11. namespace SiebelCrmConnector
  12. {
  13.     /// <summary>
  14.     /// Message inspector that holds session identification and applies it to outgoing messages
  15.     /// </summary>
  16.     public class SessionHandlingInspector : IEndpointBehavior, IClientMessageInspector
  17.     {
  18.         /// <summary>
  19.         /// Creates a new SessionHandlingInspector that will use given <paramref name="cookie"/> for authentication
  20.         /// </summary>
  21.         /// <param name="cookie">The Set-Cookie header containing the session id</param>
  22.         public SessionHandlingInspector(string cookie)
  23.         {
  24.             this.Cookie = cookie;
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Stores the Set-Cookie header for the session
  29.         /// </summary>
  30.         private string Cookie { get; set; }
  31.  
  32.         #region IEndpointBehavior Members
  33.         public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
  34.         {
  35.  
  36.         }
  37.  
  38.         /// <summary>
  39.         /// Triggered when inspector is added to an endpoint. Registeres the instance as message inspector
  40.         /// </summary>
  41.         /// <param name="endpoint"></param>
  42.         /// <param name="clientRuntime"></param>
  43.         public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  44.         {
  45.             // adds our inspector to the runtime
  46.             clientRuntime.MessageInspectors.Add(this);
  47.         }
  48.  
  49.         public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  50.         {
  51.  
  52.         }
  53.  
  54.         public void Validate(ServiceEndpoint endpoint)
  55.         {
  56.  
  57.         }
  58.         #endregion
  59.  
  60.         #region IClientMessageInspector Members
  61.         void IClientMessageInspector.AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
  62.         {
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Triggered before sending a request. Adds the cookie header to the outgoing message
  67.         /// </summary>
  68.         /// <param name="request"></param>
  69.         /// <param name="channel"></param>
  70.         /// <returns></returns>
  71.         object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
  72.         {
  73.             HttpRequestMessageProperty httpRequest;
  74.             if (!request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
  75.             {
  76.                 request.Properties.Add(HttpRequestMessageProperty.Name,
  77.                     new HttpRequestMessageProperty());
  78.             }
  79.  
  80.             httpRequest = (HttpRequestMessageProperty)
  81.                 request.Properties[HttpRequestMessageProperty.Name];
  82.             httpRequest.Headers.Add(HttpRequestHeader.Cookie, this.Cookie);
  83.  
  84.             return null;
  85.         }
  86.         #endregion
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment