Advertisement
Guest User

Enable CORS Service Behavior

a guest
Nov 27th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1.     public class EnableCrossOriginResourceSharingBehavior : IEndpointBehavior, System.ServiceModel.Dispatcher.IDispatchMessageInspector
  2.         {
  3.  
  4.             public static string AllowedMethods {get;set;}
  5.             public static string AllowedHeaders { get; set; }
  6.  
  7.             static EnableCrossOriginResourceSharingBehavior()
  8.             {
  9.                 EnableCrossOriginResourceSharingBehavior.AllowedHeaders = "GET,POST,OPTIONS";
  10.                 EnableCrossOriginResourceSharingBehavior.AllowedHeaders = "X-Requested-With,Content-Type,origin";
  11.             }
  12.             public void AddBindingParameters1(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
  13.             {
  14.             }
  15.             void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
  16.             {
  17.                 AddBindingParameters1(endpoint, bindingParameters);
  18.             }
  19.  
  20.  
  21.             public void ApplyClientBehavior1(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
  22.             {
  23.             }
  24.             void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
  25.             {
  26.                 ApplyClientBehavior1(endpoint, clientRuntime);
  27.             }
  28.  
  29.             public void ApplyDispatchBehavior1(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
  30.             {
  31.                 endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
  32.                 //          endpointDispatcher.DispatchRuntime.MessageInspectors.Add(New CustomHeaderMessageInspector(requiredHeaders))
  33.             }
  34.             void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
  35.             {
  36.                 ApplyDispatchBehavior1(endpoint, endpointDispatcher);
  37.             }
  38.  
  39.  
  40.             public void Validate1(ServiceEndpoint endpoint)
  41.             {
  42.             }
  43.             void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
  44.             {
  45.                 Validate1(endpoint);
  46.             }
  47.  
  48.             public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
  49.             {
  50.                 System.ServiceModel.Channels.HttpRequestMessageProperty httpHeader = request.Properties[System.ServiceModel.Channels.HttpRequestMessageProperty.Name] as System.ServiceModel.Channels.HttpRequestMessageProperty;
  51.                 if (httpHeader != null)
  52.                 {
  53.                     if (httpHeader.Method.ToLowerInvariant() == "options")
  54.                     {
  55.                         request.Properties.Add("is_options_header", httpHeader);
  56.                     }
  57.                 }
  58.                 return null;
  59.             }
  60.  
  61.             public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
  62.             {
  63.                 //System.Diagnostics.Debugger.Launch()
  64.                 if (reply.IsFault) return;
  65.                 var props = System.ServiceModel.OperationContext.Current.IncomingMessageProperties;
  66.                 if (props.ContainsKey("is_options_header")){
  67.                     System.ServiceModel.Channels.HttpRequestMessageProperty httpHeader =   props["is_options_header"] as System.ServiceModel.Channels.HttpRequestMessageProperty;
  68.                     System.ServiceModel.Channels.HttpResponseMessageProperty resProp = reply.Properties.Values.OfType<System.ServiceModel.Channels.HttpResponseMessageProperty>().FirstOrDefault();
  69.                     //      If respo.StatusCode = Net.HttpStatusCode.MethodNotAllowed Then
  70.                     Dictionary<string, string> requiredHeaders = new Dictionary<string, string>();
  71.                     requiredHeaders.Add("Access-Control-Allow-Origin", "*");
  72.                     requiredHeaders.Add("Access-Control-Request-Method", EnableCrossOriginResourceSharingBehavior.AllowedMethods );
  73.                     requiredHeaders.Add("Access-Control-Allow-Headers", EnableCrossOriginResourceSharingBehavior.AllowedHeaders );
  74.                     requiredHeaders.Add("Access-Control-Max-Age", TimeSpan.FromHours(1).TotalSeconds.ToString());
  75.                     requiredHeaders.Add("X-EP-Behaviour", typeof(EnableCrossOriginResourceSharingBehavior).Name);
  76.                     foreach (string item in requiredHeaders.Keys)
  77.                     {
  78.                         resProp.Headers.Add(item, requiredHeaders[item]);
  79.                     }
  80.  
  81.                     if (resProp.StatusCode == System.Net.HttpStatusCode.MethodNotAllowed)
  82.                     {
  83.                         resProp.StatusCode = System.Net.HttpStatusCode.OK;
  84.                         resProp.SuppressEntityBody = true;
  85.                     }
  86.                     //End If
  87.                 }
  88.  
  89.             }
  90.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement