-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowsCredentialsBehaviour.cs
253 lines (227 loc) · 8.97 KB
/
WindowsCredentialsBehaviour.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WindowsCredentialsBehaviour.cs" company="Solidsoft Reply Ltd.">
// Copyright (c) 2015 Solidsoft Reply Limited. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace SolidsoftReply.BizTalk.Wcf.Security
{
using System;
using System.Configuration;
using System.Security.Principal;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
/// <summary>
/// WCF behaviour to handle NTLM authentication against services in foreign domains.
/// </summary>
public class WindowsCredentialsBehaviour : BehaviorExtensionElement, IEndpointBehavior
{
/// <summary>
/// The user name.
/// </summary>
[ConfigurationProperty("Username", DefaultValue = "")]
public string Username
{
get
{
return (string)base["Username"];
}
set
{
base["Username"] = value;
}
}
/// <summary>
/// The password.
/// </summary>
[ConfigurationProperty("Password", DefaultValue = "")]
public string Password
{
get
{
return (string)base["Password"];
}
set
{
base["Password"] = value;
}
}
/// <summary>
/// The Windows domain.
/// </summary>
[ConfigurationProperty("Domain", DefaultValue = "")]
public string Domain
{
get
{
return (string)base["Domain"];
}
set
{
base["Domain"] = value;
}
}
/// <summary>
/// The Windows Impersonation level. By default, the level is Identification.
/// </summary>
[ConfigurationProperty("ImpersonationLevel", DefaultValue = TokenImpersonationLevel.Identification)]
public TokenImpersonationLevel ImpersonationLevel
{
get
{
return (TokenImpersonationLevel)base["ImpersonationLevel"];
}
set
{
base["ImpersonationLevel"] = value;
}
}
/// <summary>
/// Enables or disables the behaviour.
/// </summary>
[ConfigurationProperty("Enable", DefaultValue = true)]
public bool Enable
{
get
{
return (bool)base["Enable"];
}
set
{
base["Enable"] = value;
}
}
/// <summary>
/// Creates a behavior extension based on the current configuration settings.
/// </summary>
/// <returns>
/// The behavior extension.
/// </returns>
protected override object CreateBehavior()
{
return this;
}
/// <summary>
/// Gets the type of behavior.
/// </summary>
public override Type BehaviorType
{
get
{
return typeof(WindowsCredentialsBehaviour);
}
}
/// <summary>
/// Implement to pass data at runtime to bindings to support custom behavior.
/// </summary>
/// <param name="endpoint">The endpoint to modify.</param>
/// <param name="bindingParameters">The objects that binding elements require to support the behavior.</param>
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
if (!this.Enable)
{
return;
}
var username = this.Username;
var domain = this.Domain;
if (bindingParameters != null)
{
var clientCredentials = endpoint.Behaviors.Find<ClientCredentials>();
// If a user name was provided, then process the data to handle scenarios where the
// domain is included in the user name. Also, set the standard credentials as required.
if (!string.IsNullOrWhiteSpace(username))
{
var domainSlashIndex = username.IndexOf('\\');
// If no user credentials have already been set, set them here. This helps
// ensure that the behaviour works better when not used for Windows authentication.
// It will favour credentials set explicitly in adapter configuration, but will
// use the credentials set on the bahviour if no credentials were configured.
var setCredentails = string.IsNullOrWhiteSpace(clientCredentials.UserName.UserName);
if (domainSlashIndex >= 0)
{
if (string.IsNullOrWhiteSpace(domain) ||
domain.ToLower() == username.Substring(0, domainSlashIndex).ToLower())
{
if (setCredentails)
{
clientCredentials.UserName.UserName = username;
}
domain = username.Substring(0, domainSlashIndex).ToLower();
username = username.Substring(domainSlashIndex + 1).ToLower();
}
else
{
// Domain clash - raise error.
throw new ApplicationException(
string.Format(
SolidsoftReply.BizTalk.Wcf.Security.Properties.Resources.ExceptionDomainClash, domain, username));
}
}
else
{
if (setCredentails)
{
clientCredentials.UserName.UserName =
domain +
(string.IsNullOrWhiteSpace(domain) ? "" : @"\") +
username;
}
}
if (setCredentails)
{
clientCredentials.UserName.Password = this.Password;
}
}
// Set the Windows credentials
clientCredentials.Windows.ClientCredential.UserName = username;
clientCredentials.Windows.ClientCredential.Password = this.Password;
clientCredentials.Windows.ClientCredential.Domain = domain;
clientCredentials.Windows.AllowedImpersonationLevel = this.ImpersonationLevel;
if (bindingParameters.Find<ClientCredentials>() == null)
{
bindingParameters.Add(this);
}
}
else
{
throw new ArgumentNullException("bindingParameters");
}
}
/// <summary>
/// Implements a modification or extension of the client across an endpoint.
/// </summary>
/// <param name="endpoint">The endpoint that is to be customized.</param>
/// <param name="clientRuntime">The client runtime to be customized.</param>
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
/// <summary>
/// Implements a modification or extension of the service across an endpoint.
/// </summary>
/// <param name="endpoint">The endpoint that exposes the contract.</param>
/// <param name="endpointDispatcher">The endpoint dispatcher to be modified or extended.</param>
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
/// <summary>
/// Implement to confirm that the endpoint meets some intended criteria.
/// </summary>
/// <param name="endpoint">The endpoint to validate.</param>
void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
{
}
}
}