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