ServiceModelSecurityTokenRequirement.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security.Tokens
  5. {
  6. using System;
  7. using System.Text;
  8. using System.ServiceModel.Channels;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Description;
  11. using System.Xml;
  12. using System.ServiceModel.Security;
  13. using System.Collections.Generic;
  14. using System.IdentityModel.Tokens;
  15. using System.IdentityModel.Selectors;
  16. using System.Globalization;
  17. public abstract class ServiceModelSecurityTokenRequirement : SecurityTokenRequirement
  18. {
  19. protected const string Namespace = "http://schemas.microsoft.com/ws/2006/05/servicemodel/securitytokenrequirement";
  20. const string securityAlgorithmSuiteProperty = Namespace + "/SecurityAlgorithmSuite";
  21. const string securityBindingElementProperty = Namespace + "/SecurityBindingElement";
  22. const string issuerAddressProperty = Namespace + "/IssuerAddress";
  23. const string issuerBindingProperty = Namespace + "/IssuerBinding";
  24. const string secureConversationSecurityBindingElementProperty = Namespace + "/SecureConversationSecurityBindingElement";
  25. const string supportSecurityContextCancellationProperty = Namespace + "/SupportSecurityContextCancellation";
  26. const string messageSecurityVersionProperty = Namespace + "/MessageSecurityVersion";
  27. const string defaultMessageSecurityVersionProperty = Namespace + "/DefaultMessageSecurityVersion";
  28. const string issuerBindingContextProperty = Namespace + "/IssuerBindingContext";
  29. const string transportSchemeProperty = Namespace + "/TransportScheme";
  30. const string isInitiatorProperty = Namespace + "/IsInitiator";
  31. const string targetAddressProperty = Namespace + "/TargetAddress";
  32. const string viaProperty = Namespace + "/Via";
  33. const string listenUriProperty = Namespace + "/ListenUri";
  34. const string auditLogLocationProperty = Namespace + "/AuditLogLocation";
  35. const string suppressAuditFailureProperty = Namespace + "/SuppressAuditFailure";
  36. const string messageAuthenticationAuditLevelProperty = Namespace + "/MessageAuthenticationAuditLevel";
  37. const string isOutOfBandTokenProperty = Namespace + "/IsOutOfBandToken";
  38. const string preferSslCertificateAuthenticatorProperty = Namespace + "/PreferSslCertificateAuthenticator";
  39. // the following properties dont have top level OM properties but are part of the property bag
  40. const string supportingTokenAttachmentModeProperty = Namespace + "/SupportingTokenAttachmentMode";
  41. const string messageDirectionProperty = Namespace + "/MessageDirection";
  42. const string httpAuthenticationSchemeProperty = Namespace + "/HttpAuthenticationScheme";
  43. const string issuedSecurityTokenParametersProperty = Namespace + "/IssuedSecurityTokenParameters";
  44. const string privacyNoticeUriProperty = Namespace + "/PrivacyNoticeUri";
  45. const string privacyNoticeVersionProperty = Namespace + "/PrivacyNoticeVersion";
  46. const string duplexClientLocalAddressProperty = Namespace + "/DuplexClientLocalAddress";
  47. const string endpointFilterTableProperty = Namespace + "/EndpointFilterTable";
  48. const string channelParametersCollectionProperty = Namespace + "/ChannelParametersCollection";
  49. const string extendedProtectionPolicy = Namespace + "/ExtendedProtectionPolicy";
  50. const bool defaultSupportSecurityContextCancellation = false;
  51. protected ServiceModelSecurityTokenRequirement()
  52. : base()
  53. {
  54. this.Properties[SupportSecurityContextCancellationProperty] = defaultSupportSecurityContextCancellation;
  55. }
  56. static public string SecurityAlgorithmSuiteProperty { get { return securityAlgorithmSuiteProperty; } }
  57. static public string SecurityBindingElementProperty { get { return securityBindingElementProperty; } }
  58. static public string IssuerAddressProperty { get { return issuerAddressProperty; } }
  59. static public string IssuerBindingProperty { get { return issuerBindingProperty; } }
  60. static public string SecureConversationSecurityBindingElementProperty { get { return secureConversationSecurityBindingElementProperty; } }
  61. static public string SupportSecurityContextCancellationProperty { get { return supportSecurityContextCancellationProperty; } }
  62. static public string MessageSecurityVersionProperty { get { return messageSecurityVersionProperty; } }
  63. static internal string DefaultMessageSecurityVersionProperty { get { return defaultMessageSecurityVersionProperty; } }
  64. static public string IssuerBindingContextProperty { get { return issuerBindingContextProperty; } }
  65. static public string TransportSchemeProperty { get { return transportSchemeProperty; } }
  66. static public string IsInitiatorProperty { get { return isInitiatorProperty; } }
  67. static public string TargetAddressProperty { get { return targetAddressProperty; } }
  68. static public string ViaProperty { get { return viaProperty; } }
  69. static public string ListenUriProperty { get { return listenUriProperty; } }
  70. static public string AuditLogLocationProperty { get { return auditLogLocationProperty; } }
  71. static public string SuppressAuditFailureProperty { get { return suppressAuditFailureProperty; } }
  72. static public string MessageAuthenticationAuditLevelProperty { get { return messageAuthenticationAuditLevelProperty; } }
  73. static public string IsOutOfBandTokenProperty { get { return isOutOfBandTokenProperty; } }
  74. static public string PreferSslCertificateAuthenticatorProperty { get { return preferSslCertificateAuthenticatorProperty; } }
  75. static public string SupportingTokenAttachmentModeProperty { get { return supportingTokenAttachmentModeProperty; } }
  76. static public string MessageDirectionProperty { get { return messageDirectionProperty; } }
  77. static public string HttpAuthenticationSchemeProperty { get { return httpAuthenticationSchemeProperty; } }
  78. static public string IssuedSecurityTokenParametersProperty { get { return issuedSecurityTokenParametersProperty; } }
  79. static public string PrivacyNoticeUriProperty { get { return privacyNoticeUriProperty; } }
  80. static public string PrivacyNoticeVersionProperty { get { return privacyNoticeVersionProperty; } }
  81. static public string DuplexClientLocalAddressProperty { get { return duplexClientLocalAddressProperty; } }
  82. static public string EndpointFilterTableProperty { get { return endpointFilterTableProperty; } }
  83. static public string ChannelParametersCollectionProperty { get { return channelParametersCollectionProperty; } }
  84. static public string ExtendedProtectionPolicy { get { return extendedProtectionPolicy; } }
  85. public bool IsInitiator
  86. {
  87. get
  88. {
  89. return GetPropertyOrDefault<bool>(IsInitiatorProperty, false);
  90. }
  91. }
  92. public SecurityAlgorithmSuite SecurityAlgorithmSuite
  93. {
  94. get
  95. {
  96. return GetPropertyOrDefault<SecurityAlgorithmSuite>(SecurityAlgorithmSuiteProperty, null);
  97. }
  98. set
  99. {
  100. this.Properties[SecurityAlgorithmSuiteProperty] = value;
  101. }
  102. }
  103. public SecurityBindingElement SecurityBindingElement
  104. {
  105. get
  106. {
  107. return GetPropertyOrDefault<SecurityBindingElement>(SecurityBindingElementProperty, null);
  108. }
  109. set
  110. {
  111. this.Properties[SecurityBindingElementProperty] = value;
  112. }
  113. }
  114. public EndpointAddress IssuerAddress
  115. {
  116. get
  117. {
  118. return GetPropertyOrDefault<EndpointAddress>(IssuerAddressProperty, null);
  119. }
  120. set
  121. {
  122. this.Properties[IssuerAddressProperty] = value;
  123. }
  124. }
  125. public Binding IssuerBinding
  126. {
  127. get
  128. {
  129. return GetPropertyOrDefault<Binding>(IssuerBindingProperty, null);
  130. }
  131. set
  132. {
  133. this.Properties[IssuerBindingProperty] = value;
  134. }
  135. }
  136. public SecurityBindingElement SecureConversationSecurityBindingElement
  137. {
  138. get
  139. {
  140. return GetPropertyOrDefault<SecurityBindingElement>(SecureConversationSecurityBindingElementProperty, null);
  141. }
  142. set
  143. {
  144. this.Properties[SecureConversationSecurityBindingElementProperty] = value;
  145. }
  146. }
  147. public SecurityTokenVersion MessageSecurityVersion
  148. {
  149. get
  150. {
  151. return GetPropertyOrDefault<SecurityTokenVersion>(MessageSecurityVersionProperty, null);
  152. }
  153. set
  154. {
  155. this.Properties[MessageSecurityVersionProperty] = value;
  156. }
  157. }
  158. internal MessageSecurityVersion DefaultMessageSecurityVersion
  159. {
  160. get
  161. {
  162. MessageSecurityVersion messageSecurityVersion;
  163. return (this.TryGetProperty<MessageSecurityVersion>(DefaultMessageSecurityVersionProperty, out messageSecurityVersion)) ? messageSecurityVersion : null;
  164. }
  165. set
  166. {
  167. this.Properties[DefaultMessageSecurityVersionProperty] = (object)value;
  168. }
  169. }
  170. public string TransportScheme
  171. {
  172. get
  173. {
  174. return GetPropertyOrDefault<string>(TransportSchemeProperty, null);
  175. }
  176. set
  177. {
  178. this.Properties[TransportSchemeProperty] = value;
  179. }
  180. }
  181. internal bool SupportSecurityContextCancellation
  182. {
  183. get
  184. {
  185. return GetPropertyOrDefault<bool>(SupportSecurityContextCancellationProperty, defaultSupportSecurityContextCancellation);
  186. }
  187. set
  188. {
  189. this.Properties[SupportSecurityContextCancellationProperty] = value;
  190. }
  191. }
  192. internal EndpointAddress DuplexClientLocalAddress
  193. {
  194. get
  195. {
  196. return GetPropertyOrDefault<EndpointAddress>(duplexClientLocalAddressProperty, null);
  197. }
  198. set
  199. {
  200. this.Properties[duplexClientLocalAddressProperty] = value;
  201. }
  202. }
  203. internal TValue GetPropertyOrDefault<TValue>(string propertyName, TValue defaultValue)
  204. {
  205. TValue result;
  206. if (!TryGetProperty<TValue>(propertyName, out result))
  207. {
  208. result = defaultValue;
  209. }
  210. return result;
  211. }
  212. internal string InternalToString()
  213. {
  214. StringBuilder sb = new StringBuilder();
  215. sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "{0}:", this.GetType().ToString()));
  216. foreach (string propertyName in this.Properties.Keys)
  217. {
  218. object propertyValue = this.Properties[propertyName];
  219. sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "PropertyName: {0}", propertyName));
  220. sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "PropertyValue: {0}", propertyValue));
  221. sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "---"));
  222. }
  223. return sb.ToString().Trim();
  224. }
  225. }
  226. }