2
0

IssuedTokenClientElement.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Configuration;
  10. using System.Runtime;
  11. using System.ServiceModel;
  12. using System.ServiceModel.Description;
  13. using System.ServiceModel.Security;
  14. public sealed partial class IssuedTokenClientElement : ConfigurationElement
  15. {
  16. public IssuedTokenClientElement()
  17. {
  18. }
  19. [ConfigurationProperty(ConfigurationStrings.LocalIssuer)]
  20. public IssuedTokenParametersEndpointAddressElement LocalIssuer
  21. {
  22. get { return (IssuedTokenParametersEndpointAddressElement)base[ConfigurationStrings.LocalIssuer]; }
  23. }
  24. [ConfigurationProperty(ConfigurationStrings.LocalIssuerChannelBehaviors, DefaultValue = "")]
  25. [StringValidator(MinLength = 0)]
  26. public string LocalIssuerChannelBehaviors
  27. {
  28. get { return (string)base[ConfigurationStrings.LocalIssuerChannelBehaviors]; }
  29. set
  30. {
  31. if (String.IsNullOrEmpty(value))
  32. {
  33. value = String.Empty;
  34. }
  35. base[ConfigurationStrings.LocalIssuerChannelBehaviors] = value;
  36. }
  37. }
  38. [ConfigurationProperty(ConfigurationStrings.IssuerChannelBehaviors)]
  39. public IssuedTokenClientBehaviorsElementCollection IssuerChannelBehaviors
  40. {
  41. get { return (IssuedTokenClientBehaviorsElementCollection)base[ConfigurationStrings.IssuerChannelBehaviors]; }
  42. }
  43. [ConfigurationProperty(ConfigurationStrings.CacheIssuedTokens, DefaultValue = SpnegoTokenProvider.defaultClientCacheTokens)]
  44. public bool CacheIssuedTokens
  45. {
  46. get { return (bool)base[ConfigurationStrings.CacheIssuedTokens]; }
  47. set { base[ConfigurationStrings.CacheIssuedTokens] = value; }
  48. }
  49. [ConfigurationProperty(ConfigurationStrings.MaxIssuedTokenCachingTime, DefaultValue = SpnegoTokenProvider.defaultClientMaxTokenCachingTimeString)]
  50. [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
  51. [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanZero)]
  52. public TimeSpan MaxIssuedTokenCachingTime
  53. {
  54. get { return (TimeSpan)base[ConfigurationStrings.MaxIssuedTokenCachingTime]; }
  55. set { base[ConfigurationStrings.MaxIssuedTokenCachingTime] = value; }
  56. }
  57. [ConfigurationProperty(ConfigurationStrings.DefaultKeyEntropyMode, DefaultValue = System.ServiceModel.Security.AcceleratedTokenProvider.defaultKeyEntropyMode)]
  58. [ServiceModelEnumValidator(typeof(SecurityKeyEntropyModeHelper))]
  59. public SecurityKeyEntropyMode DefaultKeyEntropyMode
  60. {
  61. get { return (SecurityKeyEntropyMode)base[ConfigurationStrings.DefaultKeyEntropyMode]; }
  62. set { base[ConfigurationStrings.DefaultKeyEntropyMode] = value; }
  63. }
  64. [ConfigurationProperty(ConfigurationStrings.IssuedTokenRenewalThresholdPercentage, DefaultValue = SpnegoTokenProvider.defaultServiceTokenValidityThresholdPercentage)]
  65. [IntegerValidator(MinValue = 0, MaxValue = 100)]
  66. public int IssuedTokenRenewalThresholdPercentage
  67. {
  68. get { return (int)base[ConfigurationStrings.IssuedTokenRenewalThresholdPercentage]; }
  69. set { base[ConfigurationStrings.IssuedTokenRenewalThresholdPercentage] = value; }
  70. }
  71. public void Copy(IssuedTokenClientElement from)
  72. {
  73. if (this.IsReadOnly())
  74. {
  75. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
  76. }
  77. if (null == from)
  78. {
  79. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("from");
  80. }
  81. this.DefaultKeyEntropyMode = from.DefaultKeyEntropyMode;
  82. this.CacheIssuedTokens = from.CacheIssuedTokens;
  83. this.MaxIssuedTokenCachingTime = from.MaxIssuedTokenCachingTime;
  84. this.IssuedTokenRenewalThresholdPercentage = from.IssuedTokenRenewalThresholdPercentage;
  85. #pragma warning suppress 56506 //[....]; from.ElementInformation.Properties[ConfigurationStrings.LocalIssuerIssuedTokenParameters] can never be null (underlying configuration system guarantees)
  86. if (PropertyValueOrigin.Default != from.ElementInformation.Properties[ConfigurationStrings.LocalIssuer].ValueOrigin)
  87. {
  88. this.LocalIssuer.Copy(from.LocalIssuer);
  89. }
  90. #pragma warning suppress 56506 //[....]; from.ElementInformation.Properties[ConfigurationStrings.LocalIssuerChannelBehaviors] can never be null (underlying configuration system guarantees)
  91. if (PropertyValueOrigin.Default != from.ElementInformation.Properties[ConfigurationStrings.LocalIssuerChannelBehaviors].ValueOrigin)
  92. {
  93. this.LocalIssuerChannelBehaviors = from.LocalIssuerChannelBehaviors;
  94. }
  95. #pragma warning suppress 56506 //[....]; from.ElementInformation.Properties[ConfigurationStrings.IssuerChannelBehaviors] can never be null (underlying configuration system guarantees)
  96. if (PropertyValueOrigin.Default != from.ElementInformation.Properties[ConfigurationStrings.IssuerChannelBehaviors].ValueOrigin)
  97. {
  98. foreach (IssuedTokenClientBehaviorsElement element in from.IssuerChannelBehaviors)
  99. {
  100. this.IssuerChannelBehaviors.Add(element);
  101. }
  102. }
  103. }
  104. internal void ApplyConfiguration(IssuedTokenClientCredential issuedToken)
  105. {
  106. if (issuedToken == null)
  107. {
  108. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("issuedToken");
  109. }
  110. issuedToken.CacheIssuedTokens = this.CacheIssuedTokens;
  111. issuedToken.DefaultKeyEntropyMode = this.DefaultKeyEntropyMode;
  112. issuedToken.MaxIssuedTokenCachingTime = this.MaxIssuedTokenCachingTime;
  113. issuedToken.IssuedTokenRenewalThresholdPercentage = this.IssuedTokenRenewalThresholdPercentage;
  114. if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.LocalIssuer].ValueOrigin)
  115. {
  116. this.LocalIssuer.Validate();
  117. issuedToken.LocalIssuerAddress = ConfigLoader.LoadEndpointAddress(this.LocalIssuer);
  118. if (!string.IsNullOrEmpty(this.LocalIssuer.Binding))
  119. {
  120. issuedToken.LocalIssuerBinding = ConfigLoader.LookupBinding(this.LocalIssuer.Binding, this.LocalIssuer.BindingConfiguration, this.EvaluationContext);
  121. }
  122. }
  123. if (!string.IsNullOrEmpty(this.LocalIssuerChannelBehaviors))
  124. {
  125. ConfigLoader.LoadChannelBehaviors(this.LocalIssuerChannelBehaviors, this.EvaluationContext, issuedToken.LocalIssuerChannelBehaviors);
  126. }
  127. if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.IssuerChannelBehaviors].ValueOrigin)
  128. {
  129. foreach (IssuedTokenClientBehaviorsElement issuerBehaviorElement in this.IssuerChannelBehaviors)
  130. {
  131. if (!string.IsNullOrEmpty(issuerBehaviorElement.BehaviorConfiguration))
  132. {
  133. KeyedByTypeCollection<IEndpointBehavior> issuerBehaviors = new KeyedByTypeCollection<IEndpointBehavior>();
  134. ConfigLoader.LoadChannelBehaviors(issuerBehaviorElement.BehaviorConfiguration, this.EvaluationContext, issuerBehaviors);
  135. issuedToken.IssuerChannelBehaviors.Add(new Uri(issuerBehaviorElement.IssuerAddress), issuerBehaviors);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }