ServiceAuthenticationBehavior.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System;
  7. using System.Collections.ObjectModel;
  8. using System.Net;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Channels;
  11. using System.ServiceModel.Dispatcher;
  12. using System.ServiceModel.Security;
  13. using System.ServiceModel.Security.Tokens;
  14. public sealed class ServiceAuthenticationBehavior : IServiceBehavior
  15. {
  16. internal ServiceAuthenticationManager defaultServiceAuthenticationManager;
  17. ServiceAuthenticationManager serviceAuthenticationManager;
  18. AuthenticationSchemes authenticationSchemes;
  19. bool isAuthenticationManagerSet;
  20. bool isAuthenticationSchemesSet;
  21. bool isReadOnly;
  22. public ServiceAuthenticationBehavior()
  23. {
  24. this.ServiceAuthenticationManager = defaultServiceAuthenticationManager;
  25. this.authenticationSchemes = AuthenticationSchemes.None;
  26. }
  27. ServiceAuthenticationBehavior(ServiceAuthenticationBehavior other)
  28. {
  29. this.serviceAuthenticationManager = other.ServiceAuthenticationManager;
  30. this.authenticationSchemes = other.authenticationSchemes;
  31. this.isReadOnly = other.isReadOnly;
  32. this.isAuthenticationManagerSet = other.isAuthenticationManagerSet;
  33. this.isAuthenticationSchemesSet = other.isAuthenticationSchemesSet;
  34. }
  35. public ServiceAuthenticationManager ServiceAuthenticationManager
  36. {
  37. get
  38. {
  39. return this.serviceAuthenticationManager;
  40. }
  41. set
  42. {
  43. ThrowIfImmutable();
  44. this.serviceAuthenticationManager = value;
  45. this.isAuthenticationManagerSet = value != null;
  46. }
  47. }
  48. public AuthenticationSchemes AuthenticationSchemes
  49. {
  50. get
  51. {
  52. return this.authenticationSchemes;
  53. }
  54. set
  55. {
  56. ThrowIfImmutable();
  57. this.authenticationSchemes = value;
  58. this.isAuthenticationSchemesSet = true;
  59. }
  60. }
  61. public bool ShouldSerializeServiceAuthenticationManager()
  62. {
  63. return this.isAuthenticationManagerSet;
  64. }
  65. public bool ShouldSerializeAuthenticationSchemes()
  66. {
  67. return this.isAuthenticationSchemesSet;
  68. }
  69. void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  70. {
  71. }
  72. void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  73. {
  74. if (parameters == null)
  75. {
  76. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameters");
  77. }
  78. if (this.serviceAuthenticationManager != null)
  79. {
  80. // throw if bindingParameters already has a AuthenticationManager
  81. ServiceAuthenticationManager otherAuthenticationManager = parameters.Find<ServiceAuthenticationManager>();
  82. if (otherAuthenticationManager != null)
  83. {
  84. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MultipleAuthenticationManagersInServiceBindingParameters, otherAuthenticationManager)));
  85. }
  86. parameters.Add(this.serviceAuthenticationManager);
  87. }
  88. if (this.authenticationSchemes != AuthenticationSchemes.None)
  89. {
  90. // throw if bindingParameters already has an AuthenticationSchemes
  91. AuthenticationSchemesBindingParameter otherAuthenticationSchemesBindingParameter = parameters.Find<AuthenticationSchemesBindingParameter>();
  92. if (otherAuthenticationSchemesBindingParameter != null)
  93. {
  94. if (otherAuthenticationSchemesBindingParameter.AuthenticationSchemes != authenticationSchemes)
  95. {
  96. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MultipleAuthenticationSchemesInServiceBindingParameters, otherAuthenticationSchemesBindingParameter.AuthenticationSchemes)));
  97. }
  98. }
  99. else
  100. {
  101. parameters.Add(new AuthenticationSchemesBindingParameter(this.authenticationSchemes));
  102. }
  103. }
  104. }
  105. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
  106. {
  107. if (description == null)
  108. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("description"));
  109. if (serviceHostBase == null)
  110. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("serviceHostBase"));
  111. if (this.serviceAuthenticationManager == null)
  112. {
  113. return;
  114. }
  115. for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
  116. {
  117. ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
  118. if (channelDispatcher != null && !ServiceMetadataBehavior.IsHttpGetMetadataDispatcher(description, channelDispatcher))
  119. {
  120. foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
  121. {
  122. DispatchRuntime behavior = endpointDispatcher.DispatchRuntime;
  123. behavior.ServiceAuthenticationManager = this.serviceAuthenticationManager;
  124. ServiceEndpoint endpoint = FindMatchingServiceEndpoint(description, endpointDispatcher);
  125. if (endpoint != null)
  126. {
  127. bool isSecureConversationBinding = IsSecureConversationBinding(endpoint.Binding);
  128. if (isSecureConversationBinding)
  129. {
  130. SecurityStandardsManager standardsManager = GetConfiguredSecurityStandardsManager(endpoint.Binding);
  131. behavior.ServiceAuthenticationManager = new ServiceAuthenticationManagerWrapper(this.serviceAuthenticationManager, new string[] { standardsManager.SecureConversationDriver.CloseAction.Value });
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. internal ServiceAuthenticationBehavior Clone()
  139. {
  140. return new ServiceAuthenticationBehavior(this);
  141. }
  142. internal void MakeReadOnly()
  143. {
  144. this.isReadOnly = true;
  145. }
  146. void ThrowIfImmutable()
  147. {
  148. if (this.isReadOnly)
  149. {
  150. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  151. }
  152. }
  153. ServiceEndpoint FindMatchingServiceEndpoint(ServiceDescription description, EndpointDispatcher endpointDispatcher)
  154. {
  155. foreach (ServiceEndpoint endpoint in description.Endpoints)
  156. {
  157. if (endpoint.Address.Equals(endpointDispatcher.EndpointAddress))
  158. {
  159. return endpoint;
  160. }
  161. }
  162. return null;
  163. }
  164. bool IsSecureConversationBinding(Binding binding)
  165. {
  166. if (binding == null)
  167. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
  168. SecurityBindingElement securityBindingElement = binding.CreateBindingElements().Find<SecurityBindingElement>();
  169. if (securityBindingElement == null)
  170. {
  171. return false;
  172. }
  173. foreach (SecurityTokenParameters tokenParam in new SecurityTokenParametersEnumerable(securityBindingElement, true))
  174. {
  175. if (tokenParam is SecureConversationSecurityTokenParameters)
  176. {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. SecurityStandardsManager GetConfiguredSecurityStandardsManager(Binding binding)
  183. {
  184. if (binding == null)
  185. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
  186. SecurityBindingElement securityBindingElement = binding.CreateBindingElements().Find<SecurityBindingElement>();
  187. if (securityBindingElement == null)
  188. {
  189. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("binding", SR.GetString(SR.NoSecurityBindingElementFound));
  190. }
  191. return new SecurityStandardsManager(securityBindingElement.MessageSecurityVersion, new WSSecurityTokenSerializer(securityBindingElement.MessageSecurityVersion.SecurityVersion));
  192. }
  193. }
  194. }