-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathMetabaseSettingsIis7.cs
739 lines (642 loc) · 35.9 KB
/
MetabaseSettingsIis7.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
namespace System.ServiceModel.WasHosting
{
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Permissions;
using System.Security.Principal;
using System.ServiceModel.Activation;
using System.ServiceModel.Activation.Diagnostics;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Hosting;
using Microsoft.Web.Administration;
static class MetabaseSettingsIis7Constants
{
internal const string SitesSectionName = "system.applicationHost/sites";
internal const string ClientCertMapAuthenticationName = "system.webServer/security/authentication/clientCertificateMappingAuthentication";
internal const string IisClientCertMapAuthenticationName = "system.webServer/security/authentication/iisClientCertificateMappingAuthentication";
internal const string AnonymousAuthenticationSectionName = "system.webServer/security/authentication/anonymousAuthentication";
internal const string BasicAuthenticationSectionName = "system.webServer/security/authentication/basicAuthentication";
internal const string DigestAuthenticationSectionName = "system.webServer/security/authentication/digestAuthentication";
internal const string WindowsAuthenticationSectionName = "system.webServer/security/authentication/windowsAuthentication";
internal const string SecurityAccessSectionName = "system.webServer/security/access";
internal const string EnabledAttributeName = "enabled";
internal const string RealmAttributeName = "realm";
internal const string ValueAttributeName = "value";
internal const string SslFlagsAttributeName = "sslFlags";
internal const string ProviderElementName = "providers";
internal const string BindingsElementName = "bindings";
internal const string ProtocolAttributeName = "protocol";
internal const string BindingInfoAttributeName = "bindingInformation";
internal const string PathAttributeName = "path";
internal const string EnabledProtocolsAttributeName = "enabledProtocols";
internal const string NameAttributeName = "name";
internal const string ExtendedProtectionElementName = "extendedProtection";
internal const string TokenCheckingAttributeName = "tokenChecking";
internal const string FlagsAttributeName = "flags";
internal const string CommaSeparator = ",";
internal const string WebConfigGetSectionMethodName = "GetSection";
}
// MetabaseSettingsIis7 use ServerManager class to get Metabase settings. ServerManager
// does not work in Longhorn Sever/SP1 builds.
class MetabaseSettingsIis7 : MetabaseSettingsIis
{
internal MetabaseSettingsIis7()
: base()
{
if (!Iis7Helper.IsIis7)
{
DiagnosticUtility.DebugAssert("MetabaseSettingsIis7 constructor must not be called when running outside of IIS7");
throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(true);
}
PopulateSiteProperties();
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "called by MetabaseSettingsIis7 constructor")]
void PopulateSiteProperties()
{
Site site = ServerManagerWrapper.GetSite(HostingEnvironment.SiteName);
DiagnosticUtility.DebugAssert(site != null, "Unable to find site.");
//
// Build up the binding table.
//
IDictionary<string, List<string>> bindingList = ServerManagerWrapper.GetProtocolBindingTable(site);
// Convert to string arrays
foreach (KeyValuePair<string, List<string>> entry in bindingList)
{
this.Bindings.Add(entry.Key, entry.Value.ToArray());
entry.Value.Clear();
}
// Clear the temporary buffer
bindingList.Clear();
//
// Build up the protocol list.
//
string[] protocols = ServerManagerWrapper.GetEnabledProtocols(site).Split(MetabaseSettingsIis7Constants.CommaSeparator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string protocolValue in protocols)
{
string protocol = protocolValue.Trim();
protocol = protocol.ToLowerInvariant();
if (string.IsNullOrEmpty(protocol) || this.Protocols.Contains(protocol))
{
// Ignore duplicates and empty protocols
continue;
}
else if (string.Compare(protocol, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(protocol, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) == 0)
{
// Special casing HTTPS. If HTTP is enabled, it means that
// both HTTP and HTTPS are enabled.
if (this.Bindings.ContainsKey(Uri.UriSchemeHttp))
{
this.Protocols.Add(Uri.UriSchemeHttp);
}
if (this.Bindings.ContainsKey(Uri.UriSchemeHttps))
{
this.Protocols.Add(Uri.UriSchemeHttps);
}
}
else if (this.Bindings.ContainsKey(protocol))
{
// We only take the protocols that have bindings.
this.Protocols.Add(protocol);
}
}
}
protected override HostedServiceTransportSettings CreateTransportSettings(string relativeVirtualPath)
{
Debug.Print("MetabaseSettingsIis7.CreateTransportSettings() calling ServerManager.GetWebConfiguration() virtualPath: " + relativeVirtualPath);
string absolutePath = VirtualPathUtility.ToAbsolute(relativeVirtualPath, HostingEnvironmentWrapper.ApplicationVirtualPath);
Configuration config =
ServerManagerWrapper.GetWebConfiguration(
HostingEnvironment.SiteName,
absolutePath);
HostedServiceTransportSettings transportSettings = new HostedServiceTransportSettings();
ProcessAnonymousAuthentication(config, ref transportSettings);
ProcessBasicAuthentication(config, ref transportSettings);
ProcessWindowsAuthentication(config, ref transportSettings);
ProcessDigestAuthentication(config, ref transportSettings);
ProcessSecurityAccess(config, ref transportSettings);
return transportSettings;
}
void ProcessAnonymousAuthentication(Configuration config, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = ServerManagerWrapper.GetSection(config, MetabaseSettingsIis7Constants.AnonymousAuthenticationSectionName);
if ((section != null) &&
((bool)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthAnonymous;
}
}
void ProcessBasicAuthentication(Configuration config, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = ServerManagerWrapper.GetSection(config, MetabaseSettingsIis7Constants.BasicAuthenticationSectionName);
if ((section != null) &&
((bool)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthBasic;
transportSettings.Realm = (string)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.RealmAttributeName);
}
}
void ProcessWindowsAuthentication(Configuration config, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = ServerManagerWrapper.GetSection(config, MetabaseSettingsIis7Constants.WindowsAuthenticationSectionName);
if ((section != null) &&
((bool)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthNTLM;
List<string> providerList = ServerManagerWrapper.GetProviders(section, MetabaseSettingsIis7Constants.ProviderElementName,
MetabaseSettingsIis7Constants.ValueAttributeName);
if (providerList.Count != 0)
{
transportSettings.AuthProviders = providerList.ToArray();
}
}
try
{
ConfigurationElement element = section.GetChildElement(MetabaseSettingsIis7Constants.ExtendedProtectionElementName);
if (element != null)
{
ExtendedProtectionTokenChecking tokenChecking;
ExtendedProtectionFlags flags;
List<string> spnList;
ServerManagerWrapper.ReadIisExtendedProtectionPolicy(element, out tokenChecking, out flags, out spnList);
transportSettings.IisExtendedProtectionPolicy = BuildExtendedProtectionPolicy(tokenChecking, flags, spnList);
}
}
catch (COMException e)
{
// hit this exception only when IIS does not support CBT
// safe for us to igore this COMException so that services not using CBT still can be activated
// if a service does use CBT in binding, channel listener will catch it when comparing IIS setting against WCF (on CBT) and throw exception
if (DiagnosticUtility.ShouldTraceWarning)
{
TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.WebHostNoCBTSupport,
SR.TraceCodeWebHostNoCBTSupport, this, e);
}
}
}
void ProcessDigestAuthentication(Configuration config, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = ServerManagerWrapper.GetSection(config, MetabaseSettingsIis7Constants.DigestAuthenticationSectionName);
if ((section != null) &&
((bool)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthMD5;
}
}
void ProcessSecurityAccess(Configuration config, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = ServerManagerWrapper.GetSection(config, MetabaseSettingsIis7Constants.SecurityAccessSectionName);
// Check SSL Flags.
if (section != null)
{
int sslFlags = (int)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.SslFlagsAttributeName);
transportSettings.AccessSslFlags = (HttpAccessSslFlags)sslFlags;
// Clear SslMapCert field, which should not contain any useful data now.
transportSettings.AccessSslFlags &= ~(HttpAccessSslFlags.SslMapCert);
}
// Check whether IIS client certificate mapping is enabled.
section = ServerManagerWrapper.GetSection(config, MetabaseSettingsIis7Constants.IisClientCertMapAuthenticationName);
if ((section != null) &&
((bool)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AccessSslFlags |= HttpAccessSslFlags.SslMapCert;
}
else
{
// Check whether Active Directory client certification mapping is enabled.
section = ServerManagerWrapper.GetSection(config, MetabaseSettingsIis7Constants.ClientCertMapAuthenticationName);
if ((section != null) &&
((bool)ServerManagerWrapper.GetAttributeValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AccessSslFlags |= HttpAccessSslFlags.SslMapCert;
}
}
}
protected override IEnumerable<string> GetSiteApplicationPaths()
{
return ServerManagerWrapper.GetApplicationPaths();
}
// wraps calls to ServerManager with Asserts as necessary to support partial trust scenarios
[PermissionSet(SecurityAction.Assert, Unrestricted = true)]
static class ServerManagerWrapper
{
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7.PopulateSiteProperties")]
static internal Site GetSite(string name)
{
return new ServerManager().Sites[name];
}
static internal Configuration GetWebConfiguration(string siteName, string absolutePath)
{
return new ServerManager().GetWebConfiguration(siteName, absolutePath);
}
static internal ConfigurationSection GetSection(Configuration config, string sectionName)
{
return config.GetSection(sectionName);
}
static internal List<string> GetProviders(ConfigurationSection section, string providerElementName, string valueAttributeName)
{
List<string> providerList = new List<string>();
foreach (ConfigurationElement element in section.GetCollection(providerElementName))
{
providerList.Add((string)ServerManagerWrapper.GetAttributeValue(element, valueAttributeName));
}
return providerList;
}
static internal object GetAttributeValue(ConfigurationSection section, string attributeName)
{
return section.GetAttribute(attributeName).Value;
}
static internal object GetAttributeValue(ConfigurationElement element, string attributeName)
{
return element.GetAttribute(attributeName).Value;
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7.PopulateSiteProperties")]
static internal IDictionary<string, List<string>> GetProtocolBindingTable(Site site)
{
IDictionary<string, List<string>> bindingList = new Dictionary<string, List<string>>();
foreach (Microsoft.Web.Administration.Binding binding in site.Bindings)
{
string protocol = binding.Protocol.ToLowerInvariant();
string bindingInformation = binding.BindingInformation;
Debug.Print("MetabaseSettingsIis7.ctor() adding Protocol: " + protocol + " BindingInformation: " + bindingInformation);
if (!bindingList.ContainsKey(protocol))
{
bindingList.Add(protocol, new List<string>());
}
bindingList[protocol].Add(bindingInformation);
}
return bindingList;
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7.PopulateSiteProperties")]
static internal string GetEnabledProtocols(Site site)
{
Application application = site.Applications[HostingEnvironmentWrapper.ApplicationVirtualPath];
DiagnosticUtility.DebugAssert(application != null, "Unable to find application.");
return application.EnabledProtocols;
}
static internal void ReadIisExtendedProtectionPolicy(ConfigurationElement element, out ExtendedProtectionTokenChecking tokenChecking,
out ExtendedProtectionFlags flags, out List<string> spnList)
{
tokenChecking = (ExtendedProtectionTokenChecking)element.GetAttributeValue(MetabaseSettingsIis7Constants.TokenCheckingAttributeName);
flags = (ExtendedProtectionFlags)element.GetAttributeValue(MetabaseSettingsIis7Constants.FlagsAttributeName);
spnList = new List<string>();
foreach (ConfigurationElement configElement in element.GetCollection())
{
spnList.Add((string)configElement[MetabaseSettingsIis7Constants.NameAttributeName]);
}
}
static internal IEnumerable<string> GetApplicationPaths()
{
//Get the site ourselves instead of calling GetSite() because we should dispose of the ServerManager
using (ServerManager serverManager = new ServerManager())
{
List<string> appPaths = new List<string>();
Site site = serverManager.Sites[HostingEnvironment.SiteName];
foreach (Application app in site.Applications)
{
appPaths.Add(app.Path);
}
return appPaths;
}
}
}
}
// MetabaseSettingsIis7V2 use WebConfigurationManager to get Metabase settings, we depend on
// some methods which only availble in Longhorn Server/SP1 build.
class MetabaseSettingsIis7V2 : MetabaseSettingsIis
{
static MethodInfo getSectionMethod;
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7Factory.CreateMetabaseSettings")]
internal MetabaseSettingsIis7V2()
: base()
{
if (!Iis7Helper.IsIis7)
{
DiagnosticUtility.DebugAssert("MetabaseSettingsIis7V2 constructor must not be called when running outside of IIS7");
throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(true);
}
PopulateSiteProperties();
}
static internal MethodInfo GetSectionMethod
{
get
{
if (getSectionMethod == null)
{
Type type = typeof(WebConfigurationManager);
getSectionMethod = type.GetMethod(
MetabaseSettingsIis7Constants.WebConfigGetSectionMethodName,
new Type[3] { typeof(string), typeof(string), typeof(string) }
);
}
return getSectionMethod;
}
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7V2 constructor")]
void PopulateSiteProperties()
{
ConfigurationElement site = WebConfigurationManagerWrapper.GetSite(HostingEnvironment.SiteName);
//
// Build up the binding table.
//
IDictionary<string, List<string>> bindingList = WebConfigurationManagerWrapper.GetProtocolBindingTable(site);
// Convert to string arrays
foreach (KeyValuePair<string, List<string>> entry in bindingList)
{
this.Bindings.Add(entry.Key, entry.Value.ToArray());
entry.Value.Clear();
}
// Clear the temporary buffer
bindingList.Clear();
//
// Build up the protocol list.
//
string[] protocols = WebConfigurationManagerWrapper.GetEnabledProtocols(site).Split(MetabaseSettingsIis7Constants.CommaSeparator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string protocolValue in protocols)
{
string protocol = protocolValue.Trim();
protocol = protocol.ToLowerInvariant();
if (string.IsNullOrEmpty(protocol) || this.Protocols.Contains(protocol))
{
// Ignore duplicates and empty protocols
continue;
}
else if (string.Compare(protocol, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(protocol, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) == 0)
{
// Special casing HTTPS. If HTTP is enabled, it means that
// both HTTP and HTTPS are enabled.
if (this.Bindings.ContainsKey(Uri.UriSchemeHttp))
{
this.Protocols.Add(Uri.UriSchemeHttp);
}
if (this.Bindings.ContainsKey(Uri.UriSchemeHttps))
{
this.Protocols.Add(Uri.UriSchemeHttps);
}
}
else if (this.Bindings.ContainsKey(protocol))
{
// We only take the protocols that have bindings.
this.Protocols.Add(protocol);
}
}
}
protected override HostedServiceTransportSettings CreateTransportSettings(string relativeVirtualPath)
{
Debug.Print("MetabaseSettingsIis7.CreateTransportSettings() calling ServerManager.GetWebConfiguration() virtualPath: " + relativeVirtualPath);
string absolutePath = VirtualPathUtility.ToAbsolute(relativeVirtualPath, HostingEnvironment.ApplicationVirtualPath);
HostedServiceTransportSettings transportSettings = new HostedServiceTransportSettings();
string siteName = HostingEnvironment.SiteName;
ProcessAnonymousAuthentication(siteName, absolutePath, ref transportSettings);
ProcessBasicAuthentication(siteName, absolutePath, ref transportSettings);
ProcessWindowsAuthentication(siteName, absolutePath, ref transportSettings);
ProcessDigestAuthentication(siteName, absolutePath, ref transportSettings);
ProcessSecurityAccess(siteName, absolutePath, ref transportSettings);
return transportSettings;
}
void ProcessAnonymousAuthentication(string siteName, string virtualPath, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = WebConfigurationManagerWrapper.WebConfigGetSection(siteName, virtualPath, MetabaseSettingsIis7Constants.AnonymousAuthenticationSectionName);
if ((section != null) &&
((bool)WebConfigurationManagerWrapper.GetValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthAnonymous;
}
}
void ProcessBasicAuthentication(string siteName, string virtualPath, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = WebConfigurationManagerWrapper.WebConfigGetSection(siteName, virtualPath, MetabaseSettingsIis7Constants.BasicAuthenticationSectionName);
if ((section != null) &&
((bool)WebConfigurationManagerWrapper.GetValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthBasic;
transportSettings.Realm = (string)section.GetAttribute(MetabaseSettingsIis7Constants.RealmAttributeName).Value;
}
}
void ProcessWindowsAuthentication(string siteName, string virtualPath, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = WebConfigurationManagerWrapper.WebConfigGetSection(siteName, virtualPath, MetabaseSettingsIis7Constants.WindowsAuthenticationSectionName);
if ((section != null) &&
((bool)WebConfigurationManagerWrapper.GetValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthNTLM;
List<string> providerList = WebConfigurationManagerWrapper.GetProviderList(section);
if (providerList.Count != 0)
{
transportSettings.AuthProviders = providerList.ToArray();
}
// Check the CBT configuration
try
{
ConfigurationElement element = section.GetChildElement(MetabaseSettingsIis7Constants.ExtendedProtectionElementName);
if (element != null)
{
ExtendedProtectionTokenChecking tokenChecking;
ExtendedProtectionFlags flags;
List<string> spnList;
WebConfigurationManagerWrapper.ReadIisExtendedProtectionPolicy(element, out tokenChecking, out flags, out spnList);
transportSettings.IisExtendedProtectionPolicy = BuildExtendedProtectionPolicy(tokenChecking, flags, spnList);
}
}
catch (COMException e)
{
// hit this exception only when IIS does not support CBT
// safe for us to igore this COMException so that services not using CBT still can be activated
// if a service does use CBT in binding, channel listener will catch it when comparing IIS setting against WCF (on CBT) and throw exception
if (DiagnosticUtility.ShouldTraceWarning)
{
TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.WebHostNoCBTSupport,
SR.TraceCodeWebHostNoCBTSupport, this, e);
}
}
}
}
void ProcessDigestAuthentication(string siteName, string virtualPath, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = WebConfigurationManagerWrapper.WebConfigGetSection(siteName, virtualPath, MetabaseSettingsIis7Constants.DigestAuthenticationSectionName);
if ((section != null) &&
((bool)WebConfigurationManagerWrapper.GetValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AuthFlags = transportSettings.AuthFlags | AuthFlags.AuthMD5;
}
}
void ProcessSecurityAccess(string siteName, string virtualPath, ref HostedServiceTransportSettings transportSettings)
{
ConfigurationSection section = WebConfigurationManagerWrapper.WebConfigGetSection(siteName, virtualPath, MetabaseSettingsIis7Constants.SecurityAccessSectionName);
// Check SSL Flags.
if (section != null)
{
int sslFlags = (int)WebConfigurationManagerWrapper.GetValue(section, MetabaseSettingsIis7Constants.SslFlagsAttributeName);
transportSettings.AccessSslFlags = (HttpAccessSslFlags)sslFlags;
// Clear SslMapCert field, which should not contain any useful data now.
transportSettings.AccessSslFlags &= ~(HttpAccessSslFlags.SslMapCert);
}
// Check whether IIS client certificate mapping is enabled.
section = WebConfigurationManagerWrapper.WebConfigGetSection(siteName, virtualPath, MetabaseSettingsIis7Constants.IisClientCertMapAuthenticationName);
if ((section != null) &&
((bool)WebConfigurationManagerWrapper.GetValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AccessSslFlags |= HttpAccessSslFlags.SslMapCert;
}
else
{
// Check whether Active Directory client certification mapping is enabled.
section = WebConfigurationManagerWrapper.WebConfigGetSection(siteName, virtualPath, MetabaseSettingsIis7Constants.ClientCertMapAuthenticationName);
if ((section != null) &&
((bool)WebConfigurationManagerWrapper.GetValue(section, MetabaseSettingsIis7Constants.EnabledAttributeName))
)
{
transportSettings.AccessSslFlags |= HttpAccessSslFlags.SslMapCert;
}
}
}
protected override IEnumerable<string> GetSiteApplicationPaths()
{
ConfigurationElement site = WebConfigurationManagerWrapper.GetSite(HostingEnvironment.SiteName);
return WebConfigurationManagerWrapper.GetApplicationPaths(site);
}
[PermissionSet(SecurityAction.Assert, Unrestricted = true)]
static class WebConfigurationManagerWrapper
{
// Helper Method to get a site configuration
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7V2.PopulateSiteProperties")]
static internal ConfigurationElement GetSite(string siteName)
{
ConfigurationSection sitesSection = WebConfigGetSection(null, null, MetabaseSettingsIis7Constants.SitesSectionName);
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
return FindElement(sitesCollection, MetabaseSettingsIis7Constants.NameAttributeName, siteName);
}
// Helper method to find element based on an string attribute.
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by GetSite")]
static internal ConfigurationElement FindElement(ConfigurationElementCollection collection, string attributeName, string value)
{
foreach (ConfigurationElement element in collection)
{
if (String.Equals((string)element[attributeName], value, StringComparison.OrdinalIgnoreCase))
{
return element;
}
}
return null;
}
static internal ConfigurationSection WebConfigGetSection(string siteName, string virtualPath, string sectionName)
{
return (ConfigurationSection)GetSectionMethod.Invoke(null, new object[] { siteName, virtualPath, sectionName });
}
static internal object GetValue(ConfigurationSection section, string name)
{
return section[name];
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7V2.PopulateSiteProperties")]
static internal IDictionary<string, List<string>> GetProtocolBindingTable(ConfigurationElement site)
{
IDictionary<string, List<string>> bindingList = new Dictionary<string, List<string>>();
foreach (ConfigurationElement binding in site.GetCollection(MetabaseSettingsIis7Constants.BindingsElementName))
{
string protocol = ((string)binding[MetabaseSettingsIis7Constants.ProtocolAttributeName]).ToLowerInvariant();
string bindingInformation = (string)binding[MetabaseSettingsIis7Constants.BindingInfoAttributeName];
Debug.Print("MetabaseSettingsIis7V2.ctor() adding Protocol: " + protocol + " BindingInformation: " + bindingInformation);
if (!bindingList.ContainsKey(protocol))
{
bindingList.Add(protocol, new List<string>());
}
bindingList[protocol].Add(bindingInformation);
}
return bindingList;
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "Called by MetabaseSettingsIis7V2.PopulateSiteProperties")]
static internal string GetEnabledProtocols(ConfigurationElement site)
{
ConfigurationElement application = FindElement(
site.GetCollection(),
MetabaseSettingsIis7Constants.PathAttributeName,
HostingEnvironment.ApplicationVirtualPath
);
DiagnosticUtility.DebugAssert(application != null, "Unable to find application.");
return (string)application[MetabaseSettingsIis7Constants.EnabledProtocolsAttributeName];
}
static internal List<string> GetProviderList(ConfigurationElement section)
{
List<string> providerList = new List<string>();
foreach (ConfigurationElement element in section.GetCollection(MetabaseSettingsIis7Constants.ProviderElementName))
{
providerList.Add((string)element[MetabaseSettingsIis7Constants.ValueAttributeName]);
}
return providerList;
}
// translate IIS setting on extended protection to NCL object
static internal void ReadIisExtendedProtectionPolicy(ConfigurationElement element, out ExtendedProtectionTokenChecking tokenChecking,
out ExtendedProtectionFlags flags, out List<string> spnList)
{
tokenChecking = (ExtendedProtectionTokenChecking)element.GetAttributeValue(MetabaseSettingsIis7Constants.TokenCheckingAttributeName);
flags = (ExtendedProtectionFlags)element.GetAttributeValue(MetabaseSettingsIis7Constants.FlagsAttributeName);
spnList = new List<string>();
foreach (ConfigurationElement configElement in element.GetCollection())
{
spnList.Add((string)configElement[MetabaseSettingsIis7Constants.NameAttributeName]);
}
}
static internal IEnumerable<string> GetApplicationPaths(ConfigurationElement site)
{
List<string> appPaths = new List<string>();
ConfigurationElementCollection applications = site.GetCollection();
foreach (ConfigurationElement application in applications)
{
string appPath = (string)application["path"];
appPaths.Add(appPath);
}
return appPaths;
}
}
}
// Note: There is a dependency on this class name and CreateMetabaseSettings
// method name from System.ServiceModel.dll
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUninstantiatedInternalClasses,
Justification = "Instantiated by System.ServiceModel")]
internal class MetabaseSettingsIis7Factory
{
internal static MetabaseSettings CreateMetabaseSettings()
{
MethodInfo method = MetabaseSettingsIis7V2.GetSectionMethod;
if (method != null)
{
return new MetabaseSettingsIis7V2();
}
return new MetabaseSettingsIis7();
}
}
}