WSHttpSecurity.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Runtime;
  7. using System.ServiceModel.Channels;
  8. using System.ComponentModel;
  9. public sealed class WSHttpSecurity
  10. {
  11. internal const SecurityMode DefaultMode = SecurityMode.Message;
  12. SecurityMode mode;
  13. HttpTransportSecurity transportSecurity;
  14. NonDualMessageSecurityOverHttp messageSecurity;
  15. public WSHttpSecurity()
  16. : this(DefaultMode, GetDefaultHttpTransportSecurity(), new NonDualMessageSecurityOverHttp())
  17. {
  18. }
  19. internal WSHttpSecurity(SecurityMode mode, HttpTransportSecurity transportSecurity, NonDualMessageSecurityOverHttp messageSecurity)
  20. {
  21. this.mode = mode;
  22. this.transportSecurity = transportSecurity == null ? GetDefaultHttpTransportSecurity() : transportSecurity;
  23. this.messageSecurity = messageSecurity == null ? new NonDualMessageSecurityOverHttp() : messageSecurity;
  24. }
  25. internal static HttpTransportSecurity GetDefaultHttpTransportSecurity()
  26. {
  27. HttpTransportSecurity transportSecurity = new HttpTransportSecurity();
  28. transportSecurity.ClientCredentialType = HttpClientCredentialType.Windows;
  29. return transportSecurity;
  30. }
  31. public SecurityMode Mode
  32. {
  33. get { return this.mode; }
  34. set
  35. {
  36. if (!SecurityModeHelper.IsDefined(value))
  37. {
  38. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  39. }
  40. this.mode = value;
  41. }
  42. }
  43. public HttpTransportSecurity Transport
  44. {
  45. get { return this.transportSecurity; }
  46. set
  47. {
  48. if (value == null)
  49. {
  50. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  51. }
  52. this.transportSecurity = value;
  53. }
  54. }
  55. public NonDualMessageSecurityOverHttp Message
  56. {
  57. get { return this.messageSecurity; }
  58. set
  59. {
  60. if (value == null)
  61. {
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  63. }
  64. this.messageSecurity = value;
  65. }
  66. }
  67. internal void ApplyTransportSecurity(HttpsTransportBindingElement https)
  68. {
  69. if (this.mode == SecurityMode.TransportWithMessageCredential)
  70. {
  71. this.transportSecurity.ConfigureTransportProtectionOnly(https);
  72. }
  73. else
  74. {
  75. this.transportSecurity.ConfigureTransportProtectionAndAuthentication(https);
  76. }
  77. }
  78. internal static void ApplyTransportSecurity(HttpsTransportBindingElement transport, HttpTransportSecurity transportSecurity)
  79. {
  80. HttpTransportSecurity.ConfigureTransportProtectionAndAuthentication(transport, transportSecurity);
  81. }
  82. internal SecurityBindingElement CreateMessageSecurity(bool isReliableSessionEnabled, MessageSecurityVersion version)
  83. {
  84. if (this.mode == SecurityMode.Message || this.mode == SecurityMode.TransportWithMessageCredential)
  85. {
  86. return this.messageSecurity.CreateSecurityBindingElement(this.Mode == SecurityMode.TransportWithMessageCredential, isReliableSessionEnabled, version);
  87. }
  88. else
  89. {
  90. return null;
  91. }
  92. }
  93. internal static bool TryCreate(SecurityBindingElement sbe, UnifiedSecurityMode mode, HttpTransportSecurity transportSecurity, bool isReliableSessionEnabled, out WSHttpSecurity security)
  94. {
  95. security = null;
  96. NonDualMessageSecurityOverHttp messageSecurity = null;
  97. SecurityMode securityMode = SecurityMode.None;
  98. if (sbe != null)
  99. {
  100. mode &= UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential;
  101. securityMode = SecurityModeHelper.ToSecurityMode(mode);
  102. Fx.Assert(SecurityModeHelper.IsDefined(securityMode), string.Format("Invalid SecurityMode value: {0}.", mode.ToString()));
  103. if (!MessageSecurityOverHttp.TryCreate(sbe, securityMode == SecurityMode.TransportWithMessageCredential, isReliableSessionEnabled, out messageSecurity))
  104. {
  105. return false;
  106. }
  107. }
  108. else
  109. {
  110. mode &= ~(UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential);
  111. securityMode = SecurityModeHelper.ToSecurityMode(mode);
  112. }
  113. Fx.Assert(SecurityModeHelper.IsDefined(securityMode), string.Format("Invalid SecurityMode value: {0}.", securityMode.ToString()));
  114. security = new WSHttpSecurity(securityMode, transportSecurity, messageSecurity);
  115. return true;
  116. }
  117. internal bool InternalShouldSerialize()
  118. {
  119. return this.ShouldSerializeMode()
  120. || this.ShouldSerializeMessage()
  121. || this.ShouldSerializeTransport();
  122. }
  123. [EditorBrowsable(EditorBrowsableState.Never)]
  124. public bool ShouldSerializeMode()
  125. {
  126. return this.Mode != DefaultMode;
  127. }
  128. [EditorBrowsable(EditorBrowsableState.Never)]
  129. public bool ShouldSerializeMessage()
  130. {
  131. return this.Message.InternalShouldSerialize();
  132. }
  133. [EditorBrowsable(EditorBrowsableState.Never)]
  134. public bool ShouldSerializeTransport()
  135. {
  136. return this.Transport.ClientCredentialType != HttpClientCredentialType.Windows
  137. || this.Transport.ShouldSerializeProxyCredentialType()
  138. || this.Transport.ShouldSerializeRealm();
  139. }
  140. }
  141. }