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