ServiceSecurityAuditBehavior.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Dispatcher;
  11. public sealed class ServiceSecurityAuditBehavior : IServiceBehavior
  12. {
  13. internal const AuditLogLocation defaultAuditLogLocation = AuditLogLocation.Default;
  14. internal const bool defaultSuppressAuditFailure = true;
  15. internal const AuditLevel defaultServiceAuthorizationAuditLevel = AuditLevel.None;
  16. internal const AuditLevel defaultMessageAuthenticationAuditLevel = AuditLevel.None;
  17. AuditLogLocation auditLogLocation;
  18. bool suppressAuditFailure;
  19. AuditLevel serviceAuthorizationAuditLevel;
  20. AuditLevel messageAuthenticationAuditLevel;
  21. public ServiceSecurityAuditBehavior()
  22. {
  23. this.auditLogLocation = ServiceSecurityAuditBehavior.defaultAuditLogLocation;
  24. this.suppressAuditFailure = ServiceSecurityAuditBehavior.defaultSuppressAuditFailure;
  25. this.serviceAuthorizationAuditLevel = ServiceSecurityAuditBehavior.defaultServiceAuthorizationAuditLevel;
  26. this.messageAuthenticationAuditLevel = ServiceSecurityAuditBehavior.defaultMessageAuthenticationAuditLevel;
  27. }
  28. ServiceSecurityAuditBehavior(ServiceSecurityAuditBehavior behavior)
  29. {
  30. this.auditLogLocation = behavior.auditLogLocation;
  31. this.suppressAuditFailure = behavior.suppressAuditFailure;
  32. this.serviceAuthorizationAuditLevel = behavior.serviceAuthorizationAuditLevel;
  33. this.messageAuthenticationAuditLevel = behavior.messageAuthenticationAuditLevel;
  34. }
  35. public AuditLogLocation AuditLogLocation
  36. {
  37. get
  38. {
  39. return this.auditLogLocation;
  40. }
  41. set
  42. {
  43. if (!AuditLogLocationHelper.IsDefined(value))
  44. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  45. this.auditLogLocation = value;
  46. }
  47. }
  48. public bool SuppressAuditFailure
  49. {
  50. get
  51. {
  52. return this.suppressAuditFailure;
  53. }
  54. set
  55. {
  56. this.suppressAuditFailure = value;
  57. }
  58. }
  59. public AuditLevel ServiceAuthorizationAuditLevel
  60. {
  61. get
  62. {
  63. return this.serviceAuthorizationAuditLevel;
  64. }
  65. set
  66. {
  67. if (!AuditLevelHelper.IsDefined(value))
  68. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  69. this.serviceAuthorizationAuditLevel = value;
  70. }
  71. }
  72. public AuditLevel MessageAuthenticationAuditLevel
  73. {
  74. get
  75. {
  76. return this.messageAuthenticationAuditLevel;
  77. }
  78. set
  79. {
  80. if (!AuditLevelHelper.IsDefined(value))
  81. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  82. this.messageAuthenticationAuditLevel = value;
  83. }
  84. }
  85. internal ServiceSecurityAuditBehavior Clone()
  86. {
  87. return new ServiceSecurityAuditBehavior(this);
  88. }
  89. void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  90. {
  91. }
  92. void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  93. {
  94. if (parameters == null)
  95. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("parameters"));
  96. parameters.Add(this);
  97. }
  98. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
  99. {
  100. if (description == null)
  101. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("description"));
  102. if (serviceHostBase == null)
  103. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("serviceHostBase"));
  104. for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
  105. {
  106. ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
  107. if (channelDispatcher != null)
  108. {
  109. foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
  110. {
  111. if (!endpointDispatcher.IsSystemEndpoint)
  112. {
  113. DispatchRuntime behavior = endpointDispatcher.DispatchRuntime;
  114. behavior.SecurityAuditLogLocation = this.auditLogLocation;
  115. behavior.SuppressAuditFailure = this.suppressAuditFailure;
  116. behavior.ServiceAuthorizationAuditLevel = this.serviceAuthorizationAuditLevel;
  117. behavior.MessageAuthenticationAuditLevel = this.messageAuthenticationAuditLevel;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }