ServiceAuthorizationBehavior.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.ComponentModel;
  9. using System.IdentityModel.Policy;
  10. using System.Runtime.CompilerServices;
  11. using System.ServiceModel;
  12. using System.ServiceModel.Channels;
  13. using System.ServiceModel.Dispatcher;
  14. using System.Web.Security;
  15. public sealed class ServiceAuthorizationBehavior : IServiceBehavior
  16. {
  17. internal const bool DefaultImpersonateCallerForAllOperations = false;
  18. internal const bool DefaultImpersonateOnSerializingReply = false;
  19. internal const PrincipalPermissionMode DefaultPrincipalPermissionMode = PrincipalPermissionMode.UseWindowsGroups;
  20. bool impersonateCallerForAllOperations;
  21. bool impersonateOnSerializingReply;
  22. ReadOnlyCollection<IAuthorizationPolicy> externalAuthorizationPolicies;
  23. ServiceAuthorizationManager serviceAuthorizationManager;
  24. PrincipalPermissionMode principalPermissionMode;
  25. object roleProvider;
  26. bool isExternalPoliciesSet;
  27. bool isAuthorizationManagerSet;
  28. bool isReadOnly;
  29. public ServiceAuthorizationBehavior()
  30. {
  31. this.impersonateCallerForAllOperations = DefaultImpersonateCallerForAllOperations;
  32. this.impersonateOnSerializingReply = DefaultImpersonateOnSerializingReply;
  33. this.principalPermissionMode = DefaultPrincipalPermissionMode;
  34. }
  35. ServiceAuthorizationBehavior(ServiceAuthorizationBehavior other)
  36. {
  37. this.impersonateCallerForAllOperations = other.impersonateCallerForAllOperations;
  38. this.impersonateOnSerializingReply = other.impersonateOnSerializingReply;
  39. this.principalPermissionMode = other.principalPermissionMode;
  40. this.roleProvider = other.roleProvider;
  41. this.isExternalPoliciesSet = other.isExternalPoliciesSet;
  42. this.isAuthorizationManagerSet = other.isAuthorizationManagerSet;
  43. if (other.isExternalPoliciesSet || other.isAuthorizationManagerSet)
  44. {
  45. CopyAuthorizationPoliciesAndManager(other);
  46. }
  47. this.isReadOnly = other.isReadOnly;
  48. }
  49. public ReadOnlyCollection<IAuthorizationPolicy> ExternalAuthorizationPolicies
  50. {
  51. get
  52. {
  53. return this.externalAuthorizationPolicies;
  54. }
  55. set
  56. {
  57. ThrowIfImmutable();
  58. this.isExternalPoliciesSet = true;
  59. this.externalAuthorizationPolicies = value;
  60. }
  61. }
  62. public bool ShouldSerializeExternalAuthorizationPolicies()
  63. {
  64. return this.isExternalPoliciesSet;
  65. }
  66. public ServiceAuthorizationManager ServiceAuthorizationManager
  67. {
  68. get
  69. {
  70. return this.serviceAuthorizationManager;
  71. }
  72. set
  73. {
  74. ThrowIfImmutable();
  75. this.isAuthorizationManagerSet = true;
  76. this.serviceAuthorizationManager = value;
  77. }
  78. }
  79. public bool ShouldSerializeServiceAuthorizationManager()
  80. {
  81. return this.isAuthorizationManagerSet;
  82. }
  83. [DefaultValue(DefaultPrincipalPermissionMode)]
  84. public PrincipalPermissionMode PrincipalPermissionMode
  85. {
  86. get
  87. {
  88. return this.principalPermissionMode;
  89. }
  90. set
  91. {
  92. if (!PrincipalPermissionModeHelper.IsDefined(value))
  93. {
  94. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  95. }
  96. ThrowIfImmutable();
  97. this.principalPermissionMode = value;
  98. }
  99. }
  100. [DefaultValue(null)]
  101. public RoleProvider RoleProvider
  102. {
  103. get
  104. {
  105. return (RoleProvider)this.roleProvider;
  106. }
  107. set
  108. {
  109. ThrowIfImmutable();
  110. this.roleProvider = value;
  111. }
  112. }
  113. [DefaultValue(DefaultImpersonateCallerForAllOperations)]
  114. public bool ImpersonateCallerForAllOperations
  115. {
  116. get
  117. {
  118. return this.impersonateCallerForAllOperations;
  119. }
  120. set
  121. {
  122. ThrowIfImmutable();
  123. this.impersonateCallerForAllOperations = value;
  124. }
  125. }
  126. [DefaultValue(DefaultImpersonateOnSerializingReply)]
  127. public bool ImpersonateOnSerializingReply
  128. {
  129. get
  130. {
  131. return this.impersonateOnSerializingReply;
  132. }
  133. set
  134. {
  135. ThrowIfImmutable();
  136. this.impersonateOnSerializingReply = value;
  137. }
  138. }
  139. [MethodImpl(MethodImplOptions.NoInlining)]
  140. void ApplyAuthorizationPoliciesAndManager(DispatchRuntime behavior)
  141. {
  142. if (this.externalAuthorizationPolicies != null)
  143. {
  144. behavior.ExternalAuthorizationPolicies = this.externalAuthorizationPolicies;
  145. }
  146. if (this.serviceAuthorizationManager != null)
  147. {
  148. behavior.ServiceAuthorizationManager = this.serviceAuthorizationManager;
  149. }
  150. }
  151. [MethodImpl(MethodImplOptions.NoInlining)]
  152. void CopyAuthorizationPoliciesAndManager(ServiceAuthorizationBehavior other)
  153. {
  154. this.externalAuthorizationPolicies = other.externalAuthorizationPolicies;
  155. this.serviceAuthorizationManager = other.serviceAuthorizationManager;
  156. }
  157. [MethodImpl(MethodImplOptions.NoInlining)]
  158. void ApplyRoleProvider(DispatchRuntime dispatchRuntime)
  159. {
  160. dispatchRuntime.RoleProvider = (RoleProvider)this.roleProvider;
  161. }
  162. void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  163. {
  164. }
  165. void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  166. {
  167. }
  168. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
  169. {
  170. if (description == null)
  171. {
  172. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("description"));
  173. }
  174. if (serviceHostBase == null)
  175. {
  176. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("serviceHostBase"));
  177. }
  178. for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
  179. {
  180. ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
  181. if (channelDispatcher != null && !ServiceMetadataBehavior.IsHttpGetMetadataDispatcher(description, channelDispatcher))
  182. {
  183. foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
  184. {
  185. DispatchRuntime behavior = endpointDispatcher.DispatchRuntime;
  186. behavior.PrincipalPermissionMode = this.principalPermissionMode;
  187. if (!endpointDispatcher.IsSystemEndpoint)
  188. {
  189. behavior.ImpersonateCallerForAllOperations = this.impersonateCallerForAllOperations;
  190. behavior.ImpersonateOnSerializingReply = this.impersonateOnSerializingReply;
  191. }
  192. if (this.roleProvider != null)
  193. {
  194. ApplyRoleProvider(behavior);
  195. }
  196. if (this.isAuthorizationManagerSet || this.isExternalPoliciesSet)
  197. {
  198. ApplyAuthorizationPoliciesAndManager(behavior);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. internal ServiceAuthorizationBehavior Clone()
  205. {
  206. return new ServiceAuthorizationBehavior(this);
  207. }
  208. internal void MakeReadOnly()
  209. {
  210. this.isReadOnly = true;
  211. }
  212. void ThrowIfImmutable()
  213. {
  214. if (this.isReadOnly)
  215. {
  216. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  217. }
  218. }
  219. }
  220. }