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 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.IdentityModel.Diagnostics;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Security;
using System.Xml;
namespace System.IdentityModel.Configuration
{
/// <summary>
/// Defines the collection of configurable properties controlling the behavior of the Windows Identity Foundation.
/// </summary>
public class IdentityConfiguration
{
#pragma warning disable 1591
public const string DefaultServiceName = ConfigurationStrings.DefaultServiceName;
public static readonly TimeSpan DefaultMaxClockSkew = new TimeSpan(0, 5, 0);
internal const string DefaultMaxClockSkewString = "00:05:00";
public static readonly X509CertificateValidationMode DefaultCertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust;
public static readonly Type DefaultIssuerNameRegistryType = typeof(ConfigurationBasedIssuerNameRegistry);
public static readonly X509RevocationMode DefaultRevocationMode = X509RevocationMode.Online;
public static readonly StoreLocation DefaultTrustedStoreLocation = StoreLocation.LocalMachine;
#pragma warning restore 1591
ClaimsAuthenticationManager _claimsAuthenticationManager = new ClaimsAuthenticationManager();
ClaimsAuthorizationManager _claimsAuthorizationManager = new ClaimsAuthorizationManager();
bool _isInitialized;
SecurityTokenHandlerCollectionManager _securityTokenHandlerCollectionManager;
string _identityConfigurationName = DefaultServiceName;
TimeSpan _serviceMaxClockSkew = DefaultMaxClockSkew;
SecurityTokenHandlerConfiguration _serviceHandlerConfiguration;
X509Certificate2 _serviceCertificate;
List<X509Certificate2> knownCertificates;
/// <summary>
/// Initializes an instance of <see cref="IdentityConfiguration"/>
/// </summary>
public IdentityConfiguration()
{
SystemIdentityModelSection section = SystemIdentityModelSection.Current;
IdentityConfigurationElement element = (section != null) ? section.IdentityConfigurationElements.GetElement(DefaultServiceName) : null;
LoadConfiguration(element);
}
/// <summary>
/// Initializes an instance of <see cref="IdentityConfiguration"/>
/// </summary>
/// <param name="serviceCertificate">The service certificate to be used in ServiceTokenResolver and SessionSecurityTokenHandler.</param>
public IdentityConfiguration(X509Certificate2 serviceCertificate)
: this()
{
this.ServiceCertificate = serviceCertificate;
}
/// <summary>
/// Initializes an instance of <see cref="IdentityConfiguration"/>
/// </summary>
/// <param name="loadConfig">Whether or not config should be loaded.</param>
/// <exception cref="InvalidOperationException">Thrown if loadConfig is set to true but there is no
/// <System.IdentityModel> configuration element</exception>
public IdentityConfiguration(bool loadConfig)
{
if (loadConfig)
{
SystemIdentityModelSection section = SystemIdentityModelSection.Current;
if (section == null)
{
throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID7027));
}
IdentityConfigurationElement element = section.IdentityConfigurationElements.GetElement(DefaultServiceName);
LoadConfiguration(element);
}
else
{
LoadConfiguration(null);
}
}
/// <summary>
/// Initializes an instance of <see cref="IdentityConfiguration"/>
/// </summary>
/// <param name="loadConfig">Whether or not config should be loaded.</param>
/// <param name="serviceCertificate">The service certificate to be used in ServiceTokenResolver and SessionSecurityTokenHandler.</param>
/// <exception cref="InvalidOperationException">Thrown if loadConfig is set to true but there is no
/// <System.IdentityModel> configuration element</exception>
public IdentityConfiguration(bool loadConfig, X509Certificate2 serviceCertificate)
: this(loadConfig)
{
this.ServiceCertificate = serviceCertificate;
}
/// <summary>
/// Initializes an instance of <see cref="IdentityConfiguration"/>
/// </summary>
/// <param name="identityConfigurationName">The name of the <service> element from which configuration is to be loaded.</param>
/// <exception cref="InvalidOperationException">Thrown if there is no <System.IdentityModel> configuration element</exception>
/// <remarks>If this constructor is called then a System.IdentityModel config section with the provided name must exist.</remarks>
public IdentityConfiguration(string identityConfigurationName)
{
if (identityConfigurationName == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("identityConfigurationName");
}
SystemIdentityModelSection section = SystemIdentityModelSection.Current;
if (section == null)
{
throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID7027));
}
_identityConfigurationName = identityConfigurationName;
IdentityConfigurationElement element = section.IdentityConfigurationElements.GetElement(identityConfigurationName);
LoadConfiguration(element);
}
/// <summary>
/// Initializes an instance of <see cref="IdentityConfiguration"/>
/// </summary>
/// <param name="identityConfigurationName">The name of the <service> element from which configuration is to be loaded.</param>
/// <exception cref="InvalidOperationException">Thrown if there is no <System.IdentityModel> configuration element</exception>
/// <param name="serviceCertificate">Thrown if there is no <System.IdentityModel> configuration element</param>
/// <remarks>If this constructor is called then a System.IdentityModel config section with the provided name must exist.</remarks>
public IdentityConfiguration(string identityConfigurationName, X509Certificate2 serviceCertificate)
: this(identityConfigurationName)
{
this.ServiceCertificate = serviceCertificate;
}
/// <summary>
/// Gets or sets the AudienceRestriction.
/// </summary>
public AudienceRestriction AudienceRestriction
{
get { return _serviceHandlerConfiguration.AudienceRestriction; }
set { _serviceHandlerConfiguration.AudienceRestriction = value; }
}
/// <summary>
/// Gets the Caches configured.
/// </summary>
public IdentityModelCaches Caches
{
get { return _serviceHandlerConfiguration.Caches; }
set { _serviceHandlerConfiguration.Caches = value; }
}
/// <summary>
/// Gets or sets the certificate validation mode used by handlers to validate issuer certificates
/// </summary>
public X509CertificateValidationMode CertificateValidationMode
{
get { return _serviceHandlerConfiguration.CertificateValidationMode; }
set { _serviceHandlerConfiguration.CertificateValidationMode = value; }
}
/// <summary>
/// Gets or sets the certificate validator used by handlers to validate issuer certificates
/// </summary>
public X509CertificateValidator CertificateValidator
{
get { return _serviceHandlerConfiguration.CertificateValidator; }
set { _serviceHandlerConfiguration.CertificateValidator = value; }
}
/// <summary>
/// Gets or Sets the ClaimsAuthenticationManager.
/// </summary>
public ClaimsAuthenticationManager ClaimsAuthenticationManager
{
get { return _claimsAuthenticationManager; }
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
_claimsAuthenticationManager = value;
}
}
/// <summary>
/// Gets or Sets the ClaimsAuthorizationManager.
/// </summary>
public ClaimsAuthorizationManager ClaimsAuthorizationManager
{
get { return _claimsAuthorizationManager; }
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
_claimsAuthorizationManager = value;
}
}
/// <summary>
/// Gets or Sets detection of replaying of tokens by handlers in the default handler configuration.
/// </summary>
public bool DetectReplayedTokens
{
get { return _serviceHandlerConfiguration.DetectReplayedTokens; }
set { _serviceHandlerConfiguration.DetectReplayedTokens = value; }
}
/// <summary>
/// Determines if <see cref="IdentityConfiguration.Initialize"/> has been called.
/// </summary>
public virtual bool IsInitialized
{
get
{
return _isInitialized;
}
protected set
{
_isInitialized = value;
}
}
private static SecurityTokenResolver GetServiceTokenResolver(IdentityConfigurationElement element)
{
try
{
return CustomTypeElement.Resolve<SecurityTokenResolver>(element.ServiceTokenResolver);
}
catch (ArgumentException inner)
{
throw DiagnosticUtility.ThrowHelperConfigurationError(
element, ConfigurationStrings.ServiceTokenResolver, inner);
}
}
private static SecurityTokenResolver GetIssuerTokenResolver(IdentityConfigurationElement element)
{
try
{
return CustomTypeElement.Resolve<SecurityTokenResolver>(element.IssuerTokenResolver);
}
catch (ArgumentException inner)
{
throw DiagnosticUtility.ThrowHelperConfigurationError(
element, ConfigurationStrings.IssuerTokenResolver, inner);
}
}
private static ClaimsAuthenticationManager GetClaimsAuthenticationManager(IdentityConfigurationElement element)
{
try
{
return CustomTypeElement.Resolve<ClaimsAuthenticationManager>(element.ClaimsAuthenticationManager);
}
catch (ArgumentException inner)
{
throw DiagnosticUtility.ThrowHelperConfigurationError(
element, ConfigurationStrings.ClaimsAuthenticationManager, inner);
}
}
private static IssuerNameRegistry GetIssuerNameRegistry(IssuerNameRegistryElement element)
{
try
{
Type type = string.IsNullOrEmpty(element.Type) ? DefaultIssuerNameRegistryType : Type.GetType(element.Type);
return TypeResolveHelper.Resolve<IssuerNameRegistry>(element, type);
}
catch (ArgumentException inner)
{
throw DiagnosticUtility.ThrowHelperConfigurationError(
element, ConfigurationStrings.IssuerNameRegistry, inner);
}
}
/// <summary>
/// Updates properties in the <see cref="SecurityTokenHandlerConfiguration"/> objects for the
/// <see cref="SecurityTokenHandlerCollection"/> objects contained in
/// <see cref="IdentityConfiguration.SecurityTokenHandlerCollectionManager"/> to be consistent with the property
/// values on this <see cref="IdentityConfiguration"/> instance.
/// </summary>
/// <remarks>
/// This method should be invoked prior to using these token handlers
/// for token processing.
/// </remarks>
/// <exception cref="InvalidOperationException">If this method is invoked more than once.</exception>
public virtual void Initialize()
{
if (this.IsInitialized)
{
throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID7009));
}
SecurityTokenHandlerCollection defaultCollection = this.SecurityTokenHandlers;
if (!object.ReferenceEquals(_serviceHandlerConfiguration, defaultCollection.Configuration))
{
//
// If someone has created their own new STHConfig and set it as default, leave that config alone.
//
TraceUtility.TraceString(TraceEventType.Information, SR.GetString(SR.ID4283));
this.IsInitialized = true;
return;
}
// Update the ServiceTokenResolver of the default TokenHandlerCollection's configuration, if serviceCertificate is set.
if (this.ServiceCertificate != null)
{
SecurityTokenResolver serviceCertificateResolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(new ReadOnlyCollection<SecurityToken>(
new SecurityToken[] { new X509SecurityToken(this.ServiceCertificate) }), false);
SecurityTokenResolver tokenResolver = this.SecurityTokenHandlers.Configuration.ServiceTokenResolver;
if ((tokenResolver != null) && (tokenResolver != EmptySecurityTokenResolver.Instance))
{
this.SecurityTokenHandlers.Configuration.ServiceTokenResolver = new AggregateTokenResolver(new SecurityTokenResolver[] { serviceCertificateResolver, tokenResolver });
}
else
{
this.SecurityTokenHandlers.Configuration.ServiceTokenResolver = serviceCertificateResolver;
}
}
SecurityTokenResolver configuredIssuerTokenResolver = this.IssuerTokenResolver;
if (this.IssuerTokenResolver == SecurityTokenHandlerConfiguration.DefaultIssuerTokenResolver)
{
//
// Add the known certificates from WCF's ServiceCredentials in front of
// the default issuer token resolver.
//
if (this.KnownIssuerCertificates != null)
{
int count = this.KnownIssuerCertificates.Count;
if (count > 0)
{
SecurityToken[] tokens = new SecurityToken[count];
for (int i = 0; i < count; i++)
{
tokens[i] = new X509SecurityToken(this.KnownIssuerCertificates[i]);
}
SecurityTokenResolver knownCertificateTokenResolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(new ReadOnlyCollection<SecurityToken>(tokens), false);
this.IssuerTokenResolver = new AggregateTokenResolver(new SecurityTokenResolver[] { knownCertificateTokenResolver, configuredIssuerTokenResolver });
}
}
}
if (this.CertificateValidationMode != X509CertificateValidationMode.Custom)
{
defaultCollection.Configuration.CertificateValidator = X509Util.CreateCertificateValidator(defaultCollection.Configuration.CertificateValidationMode,
defaultCollection.Configuration.RevocationMode,
defaultCollection.Configuration.TrustedStoreLocation);
}
else if (object.ReferenceEquals(defaultCollection.Configuration.CertificateValidator, SecurityTokenHandlerConfiguration.DefaultCertificateValidator))
{
//
// If the mode is custom but the validator or still default, something has gone wrong.
//
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID4280)));
}
this.IsInitialized = true;
}
/// <summary>
/// Loads the settings for the IdentityConfiguration from the application or web configuration file.
/// </summary>
/// <remarks>
/// If there is no configuration file, or the named section does not exist, then no exception is thrown,
/// instead the class is loaded with a set of default values.
/// </remarks>
protected void LoadConfiguration(IdentityConfigurationElement element)
{
if (element != null)
{
//
// Load the claims authentication manager
//
if (element.ClaimsAuthenticationManager.IsConfigured)
{
_claimsAuthenticationManager = GetClaimsAuthenticationManager(element);
}
//
// Load the claims authorization manager.
//
if (element.ClaimsAuthorizationManager.IsConfigured)
{
_claimsAuthorizationManager = CustomTypeElement.Resolve<ClaimsAuthorizationManager>(element.ClaimsAuthorizationManager);
}
//
// Load the service level Security Token Handler configuration
//
_serviceHandlerConfiguration = LoadHandlerConfiguration(element);
}
//
// Reads handler configuration via LoadConfiguredHandlers. Do this last.
//
_securityTokenHandlerCollectionManager = LoadHandlers(element);
}
/// <summary>
/// Loads the <see cref="SecurityTokenHandlerCollectionManager"/> defined for a given service.
/// </summary>
/// <param name="serviceElement">The <see cref="IdentityConfigurationElement"/> used to configure this instance.</param>
/// <returns></returns>
protected SecurityTokenHandlerCollectionManager LoadHandlers(IdentityConfigurationElement serviceElement)
{
//
// We start with a token handler collection manager that contains a single collection that includes the default
// handlers for the system.
//
SecurityTokenHandlerCollectionManager manager = SecurityTokenHandlerCollectionManager.CreateEmptySecurityTokenHandlerCollectionManager();
if (serviceElement != null)
{
//
// Load any token handler collections that appear as part of this service element
//
if (serviceElement.SecurityTokenHandlerSets.Count > 0)
{
foreach (SecurityTokenHandlerElementCollection handlerElementCollection in serviceElement.SecurityTokenHandlerSets)
{
try
{
SecurityTokenHandlerConfiguration handlerConfiguration;
SecurityTokenHandlerCollection handlerCollection;
if (string.IsNullOrEmpty(handlerElementCollection.Name) ||
StringComparer.Ordinal.Equals(handlerElementCollection.Name, ConfigurationStrings.DefaultConfigurationElementName))
{
//
// For the default collection, merge the IdentityConfiguration with the underlying config, if it exists.
//
if (handlerElementCollection.SecurityTokenHandlerConfiguration.IsConfigured)
{
//
// Configuration from a nested configuration object. We start with Service level configuration for
// handlers and then override the collection specific configuration. The result is a new configuration
// object that can only be modified by accessing the collection or handlers configuration properties.
//
_serviceHandlerConfiguration = LoadHandlerConfiguration(serviceElement);
handlerConfiguration = LoadHandlerConfiguration(_serviceHandlerConfiguration, handlerElementCollection.SecurityTokenHandlerConfiguration);
}
else
{
//
// No nested configuration object. We use the values from the ServiceElement for this case.
//
handlerConfiguration = LoadHandlerConfiguration(serviceElement);
}
_serviceHandlerConfiguration = handlerConfiguration;
}
else
{
//
// This is a non-default collection. There should be no settings inherited from IdentityConfiguration.
//
if (handlerElementCollection.SecurityTokenHandlerConfiguration.IsConfigured)
{
handlerConfiguration = LoadHandlerConfiguration(null, handlerElementCollection.SecurityTokenHandlerConfiguration);
}
else
{
//
// If there is no underlying config, set everything as default.
//
handlerConfiguration = new SecurityTokenHandlerConfiguration();
}
}
handlerCollection = new SecurityTokenHandlerCollection(handlerConfiguration);
manager[handlerElementCollection.Name] = handlerCollection;
foreach (CustomTypeElement handlerElement in handlerElementCollection)
{
handlerCollection.Add(CustomTypeElement.Resolve<SecurityTokenHandler>(handlerElement));
}
}
catch (ArgumentException inner)
{
throw DiagnosticUtility.ThrowHelperConfigurationError(serviceElement, handlerElementCollection.Name, inner);
}
}
}
//
// Ensure that the default usage collection always exists
//
if (!manager.ContainsKey(SecurityTokenHandlerCollectionManager.Usage.Default))
{
manager[SecurityTokenHandlerCollectionManager.Usage.Default] = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection(_serviceHandlerConfiguration);
}
}
else
{
//
// Ensure that the default usage collection always exists
//
_serviceHandlerConfiguration = new SecurityTokenHandlerConfiguration();
_serviceHandlerConfiguration.MaxClockSkew = _serviceMaxClockSkew;
if (!manager.ContainsKey(SecurityTokenHandlerCollectionManager.Usage.Default))
{
manager[SecurityTokenHandlerCollectionManager.Usage.Default] = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection(_serviceHandlerConfiguration);
}
}
return manager;
}
/// <summary>
/// Loads a SecurityTokenHandlerConfiguration using the elements directly under the ServiceElement.
/// </summary>
protected SecurityTokenHandlerConfiguration LoadHandlerConfiguration(IdentityConfigurationElement element)
{
SecurityTokenHandlerConfiguration handlerConfiguration = new SecurityTokenHandlerConfiguration();
try
{
if (element.ElementInformation.Properties[ConfigurationStrings.MaximumClockSkew].ValueOrigin != PropertyValueOrigin.Default)
{
handlerConfiguration.MaxClockSkew = element.MaximumClockSkew;
}
else
{
handlerConfiguration.MaxClockSkew = _serviceMaxClockSkew;
}
}
catch (ArgumentException inner)
{
throw DiagnosticUtility.ThrowHelperConfigurationError(element, ConfigurationStrings.MaximumClockSkew, inner);
}
if (element.AudienceUris.IsConfigured)
{
handlerConfiguration.AudienceRestriction.AudienceMode = element.AudienceUris.Mode;
foreach (AudienceUriElement audienceUriElement in element.AudienceUris)
{
handlerConfiguration.AudienceRestriction.AllowedAudienceUris.Add(new Uri(audienceUriElement.Value, UriKind.RelativeOrAbsolute));
}
}
if (element.Caches.IsConfigured)
{
if (element.Caches.TokenReplayCache.IsConfigured)
{
handlerConfiguration.Caches.TokenReplayCache = CustomTypeElement.Resolve<TokenReplayCache>(element.Caches.TokenReplayCache);
}
if (element.Caches.SessionSecurityTokenCache.IsConfigured)
{
handlerConfiguration.Caches.SessionSecurityTokenCache = CustomTypeElement.Resolve<SessionSecurityTokenCache>(element.Caches.SessionSecurityTokenCache);
}
}
if (element.CertificateValidation.IsConfigured)
{
handlerConfiguration.RevocationMode = element.CertificateValidation.RevocationMode;
handlerConfiguration.CertificateValidationMode = element.CertificateValidation.CertificateValidationMode;
handlerConfiguration.TrustedStoreLocation = element.CertificateValidation.TrustedStoreLocation;
if (element.CertificateValidation.CertificateValidator.IsConfigured)
{
handlerConfiguration.CertificateValidator = CustomTypeElement.Resolve<X509CertificateValidator>(element.CertificateValidation.CertificateValidator);
}
}
//
// Load the issuer name registry
//
if (element.IssuerNameRegistry.IsConfigured)
{
handlerConfiguration.IssuerNameRegistry = GetIssuerNameRegistry(element.IssuerNameRegistry);
}
//
// Load the issuer token resolver
//
if (element.IssuerTokenResolver.IsConfigured)
{
handlerConfiguration.IssuerTokenResolver = GetIssuerTokenResolver(element);
}
//
// SaveBootstrapContext
//
handlerConfiguration.SaveBootstrapContext = element.SaveBootstrapContext;
//
// Load the service token resolver
//
if (element.ServiceTokenResolver.IsConfigured)
{
handlerConfiguration.ServiceTokenResolver = GetServiceTokenResolver(element);
}
//
// TokenReplayCache related items
//
if (element.TokenReplayDetection.IsConfigured)
{
//
// Set on SecurityTokenHandlerConfiguration
//
// DetectReplayedTokens set - { true | false }
//
handlerConfiguration.DetectReplayedTokens = element.TokenReplayDetection.Enabled;
// ExpirationPeriod { TimeSpan }
//
handlerConfiguration.TokenReplayCacheExpirationPeriod = element.TokenReplayDetection.ExpirationPeriod;
}
return handlerConfiguration;
}
/// <summary>
/// Loads configuration elements pertaining to the <see cref="SecurityTokenHandlerCollection"/>
/// </summary>
/// <param name="baseConfiguration">Base <see cref="SecurityTokenHandlerConfiguration"/> from which to inherit default values.</param>
/// <param name="element">The <see cref="SecurityTokenHandlerConfigurationElement"/> from the configuration file.</param>
/// <returns></returns>
protected SecurityTokenHandlerConfiguration LoadHandlerConfiguration(SecurityTokenHandlerConfiguration baseConfiguration, SecurityTokenHandlerConfigurationElement element)
{
SecurityTokenHandlerConfiguration handlerConfiguration = (baseConfiguration == null) ? new SecurityTokenHandlerConfiguration() : baseConfiguration;
if (element.AudienceUris.IsConfigured)
{
//
// There is no inheritance of the content of the element from base to child, only the whole element. If the
// user specifies any part, they must specify it all.
//
handlerConfiguration.AudienceRestriction.AudienceMode = AudienceUriMode.Always;
handlerConfiguration.AudienceRestriction.AllowedAudienceUris.Clear();
handlerConfiguration.AudienceRestriction.AudienceMode = element.AudienceUris.Mode;
foreach (AudienceUriElement audienceUriElement in element.AudienceUris)
{
handlerConfiguration.AudienceRestriction.AllowedAudienceUris.Add(new Uri(audienceUriElement.Value, UriKind.RelativeOrAbsolute));
}
}
if (element.Caches.IsConfigured)
{
if (element.Caches.TokenReplayCache.IsConfigured)
{
handlerConfiguration.Caches.TokenReplayCache = CustomTypeElement.Resolve<TokenReplayCache>(element.Caches.TokenReplayCache);
}
if (element.Caches.SessionSecurityTokenCache.IsConfigured)
{
handlerConfiguration.Caches.SessionSecurityTokenCache = CustomTypeElement.Resolve<SessionSecurityTokenCache>(element.Caches.SessionSecurityTokenCache);
}
}
if (element.CertificateValidation.IsConfigured)
{
handlerConfiguration.RevocationMode = element.CertificateValidation.RevocationMode;
handlerConfiguration.CertificateValidationMode = element.CertificateValidation.CertificateValidationMode;
handlerConfiguration.TrustedStoreLocation = element.CertificateValidation.TrustedStoreLocation;
if (element.CertificateValidation.CertificateValidator.IsConfigured)
{
handlerConfiguration.CertificateValidator = CustomTypeElement.Resolve<X509CertificateValidator>(element.CertificateValidation.CertificateValidator);
}
}
//
// Load the issuer name registry
//
if (element.IssuerNameRegistry.IsConfigured)
{
handlerConfiguration.IssuerNameRegistry = GetIssuerNameRegistry(element.IssuerNameRegistry);
}
//
// Load the issuer token resolver
//
if (element.IssuerTokenResolver.IsConfigured)
{
handlerConfiguration.IssuerTokenResolver = CustomTypeElement.Resolve<SecurityTokenResolver>(element.IssuerTokenResolver);
}
//
// Load MaxClockSkew
//
try
{
if (element.ElementInformation.Properties[ConfigurationStrings.MaximumClockSkew].ValueOrigin != PropertyValueOrigin.Default)
{
handlerConfiguration.MaxClockSkew = element.MaximumClockSkew;
}
}
catch (ArgumentException inner)
{
throw DiagnosticUtility.ThrowHelperConfigurationError(element, ConfigurationStrings.MaximumClockSkew, inner);
}
//
// SaveBootstrapTokens
//
if (element.ElementInformation.Properties[ConfigurationStrings.SaveBootstrapContext].ValueOrigin != PropertyValueOrigin.Default)
{
handlerConfiguration.SaveBootstrapContext = element.SaveBootstrapContext;
}
//
// Load the service token resolver
//
if (element.ServiceTokenResolver.IsConfigured)
{
handlerConfiguration.ServiceTokenResolver = CustomTypeElement.Resolve<SecurityTokenResolver>(element.ServiceTokenResolver);
}
//
// TokenReplayCache related items
//
if (element.TokenReplayDetection.IsConfigured)
{
//
// Set on SecurityTokenHandlerConfiguration
//
//
// DetectReplayedTokens set - { true | false }
//
handlerConfiguration.DetectReplayedTokens = element.TokenReplayDetection.Enabled;
//
// ExpirationPeriod { TimeSpan }
//
handlerConfiguration.TokenReplayCacheExpirationPeriod = element.TokenReplayDetection.ExpirationPeriod;
}
return handlerConfiguration;
}
/// <summary>
/// Gets or sets the maximum allowable time difference between the
/// system clocks of the two parties that are communicating.
/// </summary>
public TimeSpan MaxClockSkew
{
get { return _serviceHandlerConfiguration.MaxClockSkew; }
set { _serviceHandlerConfiguration.MaxClockSkew = value; }
}
/// <summary>
/// Gets or sets the service name of this configuration.
/// </summary>
public string Name
{
get
{
return _identityConfigurationName;
}
}
/// <summary>
/// Gets or sets the IssuerNameRegistry used to resolve issuer names.
/// </summary>
public IssuerNameRegistry IssuerNameRegistry
{
get
{
return _serviceHandlerConfiguration.IssuerNameRegistry;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
_serviceHandlerConfiguration.IssuerNameRegistry = value;
}
}
/// <summary>
/// The service certificate to initialize the ServiceTokenResolver and the SessionSecurityTokenHandler.
/// </summary>
public X509Certificate2 ServiceCertificate
{
get { return _serviceCertificate; }
set { this._serviceCertificate = value; }
}
internal List<X509Certificate2> KnownIssuerCertificates
{
get
{
return this.knownCertificates;
}
set
{
this.knownCertificates = value;
}
}
/// <summary>
/// Gets or Sets the Issuer token resolver.
/// </summary>
public SecurityTokenResolver IssuerTokenResolver
{
get
{
return _serviceHandlerConfiguration.IssuerTokenResolver;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
_serviceHandlerConfiguration.IssuerTokenResolver = value;
}
}
/// <summary>
/// Gets or sets the revocation mode used by handlers to validate issuer certificates
/// </summary>
public X509RevocationMode RevocationMode
{
get { return _serviceHandlerConfiguration.RevocationMode; }
set { _serviceHandlerConfiguration.RevocationMode = value; }
}
/// <summary>
/// Gets or Sets the Service token resolver.
/// </summary>
public SecurityTokenResolver ServiceTokenResolver
{
get
{
return _serviceHandlerConfiguration.ServiceTokenResolver;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
_serviceHandlerConfiguration.ServiceTokenResolver = value;
}
}
/// <summary>
/// Gets or sets if BootstrapContext is saved in the ClaimsIdentity and Sessions after token validation.
/// </summary>
public bool SaveBootstrapContext
{
get { return _serviceHandlerConfiguration.SaveBootstrapContext; }
set { _serviceHandlerConfiguration.SaveBootstrapContext = value; }
}
/// <summary>
/// The <see cref="SecurityTokenHandlerCollectionManager" /> containing the set of <see cref="SecurityTokenHandler" />
/// objects used for serializing and validating tokens found in WS-Trust messages.
/// </summary>
public SecurityTokenHandlerCollectionManager SecurityTokenHandlerCollectionManager
{
get
{
return _securityTokenHandlerCollectionManager;
}
}
/// <summary>
/// The <see cref="SecurityTokenHandlerCollection" /> collection of <see cref="SecurityTokenHandler" />
/// objects used for serializing and validating tokens found in WS-Trust messages.
/// If user wants to register their own token handler, they
/// can simply add their own handler to this collection.
/// </summary>
public SecurityTokenHandlerCollection SecurityTokenHandlers
{
get
{
return _securityTokenHandlerCollectionManager[SecurityTokenHandlerCollectionManager.Usage.Default];
}
}
/// <summary>
/// Gets or Sets the expiration period for items placed in the TokenReplayCache.
/// </summary>
public TimeSpan TokenReplayCacheExpirationPeriod
{
get { return _serviceHandlerConfiguration.TokenReplayCacheExpirationPeriod; }
set { _serviceHandlerConfiguration.TokenReplayCacheExpirationPeriod = value; }
}
/// <summary>
/// Gets or sets the trusted store location used by handlers to validate issuer certificates
/// </summary>
public StoreLocation TrustedStoreLocation
{
get { return _serviceHandlerConfiguration.TrustedStoreLocation; }
set { _serviceHandlerConfiguration.TrustedStoreLocation = value; }
}
}
}
|