BasicHttpContextBinding.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.ComponentModel;
  8. using System.Configuration;
  9. using System.Net.Security;
  10. using System.Runtime.CompilerServices;
  11. using System.ServiceModel.Channels;
  12. using System.ServiceModel.Configuration;
  13. [TypeForwardedFrom("System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  14. public class BasicHttpContextBinding : BasicHttpBinding
  15. {
  16. bool contextManagementEnabled = ContextBindingElement.DefaultContextManagementEnabled;
  17. public BasicHttpContextBinding()
  18. : base()
  19. {
  20. this.AllowCookies = true;
  21. }
  22. public BasicHttpContextBinding(BasicHttpSecurityMode securityMode)
  23. : base(securityMode)
  24. {
  25. this.AllowCookies = true;
  26. }
  27. public BasicHttpContextBinding(string configName)
  28. : base()
  29. {
  30. if (configName == null)
  31. {
  32. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("configName");
  33. }
  34. BasicHttpContextBindingCollectionElement section = BasicHttpContextBindingCollectionElement.GetBindingCollectionElement();
  35. BasicHttpContextBindingElement element = section.Bindings[configName];
  36. element.ApplyConfiguration(this);
  37. if (element.ElementInformation.Properties["allowCookies"].ValueOrigin == PropertyValueOrigin.Default)
  38. {
  39. this.AllowCookies = true;
  40. }
  41. else if (!this.AllowCookies)
  42. {
  43. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.BasicHttpContextBindingRequiresAllowCookie, this.Namespace, this.Name));
  44. }
  45. }
  46. [DefaultValue(ContextBindingElement.DefaultContextManagementEnabled)]
  47. public bool ContextManagementEnabled
  48. {
  49. get
  50. {
  51. return this.contextManagementEnabled;
  52. }
  53. set
  54. {
  55. this.contextManagementEnabled = value;
  56. }
  57. }
  58. public override BindingElementCollection CreateBindingElements()
  59. {
  60. if (!this.AllowCookies)
  61. {
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BasicHttpContextBindingRequiresAllowCookie, this.Namespace, this.Name)));
  63. }
  64. BindingElementCollection result;
  65. try
  66. {
  67. // Passing AllowCookies=false to HttpTransportBinding means we don't want transport layer to manage
  68. // cookie containers. We are going to do this at the context channel level, because we need channel
  69. // level isolation as opposed to channel factory level isolation.
  70. this.AllowCookies = false;
  71. result = base.CreateBindingElements();
  72. }
  73. finally
  74. {
  75. this.AllowCookies = true;
  76. }
  77. result.Insert(0, new ContextBindingElement(ProtectionLevel.None, ContextExchangeMechanism.HttpCookie, null, this.ContextManagementEnabled));
  78. return result;
  79. }
  80. }
  81. }