IssuedTokenClientCredential.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Collections.Generic;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Description;
  11. public sealed class IssuedTokenClientCredential
  12. {
  13. SecurityKeyEntropyMode defaultKeyEntropyMode = AcceleratedTokenProvider.defaultKeyEntropyMode;
  14. KeyedByTypeCollection<IEndpointBehavior> localIssuerChannelBehaviors;
  15. Dictionary<Uri, KeyedByTypeCollection<IEndpointBehavior>> issuerChannelBehaviors;
  16. bool cacheIssuedTokens = SpnegoTokenProvider.defaultClientCacheTokens;
  17. TimeSpan maxIssuedTokenCachingTime = SpnegoTokenProvider.DefaultClientMaxTokenCachingTime;
  18. EndpointAddress localIssuerAddress;
  19. Binding localIssuerBinding;
  20. int issuedTokenRenewalThresholdPercentage = AcceleratedTokenProvider.defaultServiceTokenValidityThresholdPercentage;
  21. bool isReadOnly;
  22. internal IssuedTokenClientCredential()
  23. {
  24. }
  25. internal IssuedTokenClientCredential(IssuedTokenClientCredential other)
  26. {
  27. this.defaultKeyEntropyMode = other.defaultKeyEntropyMode;
  28. this.cacheIssuedTokens = other.cacheIssuedTokens;
  29. this.issuedTokenRenewalThresholdPercentage = other.issuedTokenRenewalThresholdPercentage;
  30. this.maxIssuedTokenCachingTime = other.maxIssuedTokenCachingTime;
  31. this.localIssuerAddress = other.localIssuerAddress;
  32. this.localIssuerBinding = (other.localIssuerBinding != null) ? new CustomBinding(other.localIssuerBinding) : null;
  33. if (other.localIssuerChannelBehaviors != null)
  34. this.localIssuerChannelBehaviors = GetBehaviorCollection(other.localIssuerChannelBehaviors);
  35. if (other.issuerChannelBehaviors != null)
  36. {
  37. this.issuerChannelBehaviors = new Dictionary<Uri, KeyedByTypeCollection<IEndpointBehavior>>();
  38. foreach (Uri uri in other.issuerChannelBehaviors.Keys)
  39. {
  40. this.issuerChannelBehaviors.Add(uri, GetBehaviorCollection(other.issuerChannelBehaviors[uri]));
  41. }
  42. }
  43. this.isReadOnly = other.isReadOnly;
  44. }
  45. public EndpointAddress LocalIssuerAddress
  46. {
  47. get
  48. {
  49. return this.localIssuerAddress;
  50. }
  51. set
  52. {
  53. ThrowIfImmutable();
  54. this.localIssuerAddress = value;
  55. }
  56. }
  57. public Binding LocalIssuerBinding
  58. {
  59. get
  60. {
  61. return this.localIssuerBinding;
  62. }
  63. set
  64. {
  65. ThrowIfImmutable();
  66. this.localIssuerBinding = value;
  67. }
  68. }
  69. public SecurityKeyEntropyMode DefaultKeyEntropyMode
  70. {
  71. get
  72. {
  73. return this.defaultKeyEntropyMode;
  74. }
  75. set
  76. {
  77. SecurityKeyEntropyModeHelper.Validate(value);
  78. ThrowIfImmutable();
  79. this.defaultKeyEntropyMode = value;
  80. }
  81. }
  82. public bool CacheIssuedTokens
  83. {
  84. get
  85. {
  86. return this.cacheIssuedTokens;
  87. }
  88. set
  89. {
  90. ThrowIfImmutable();
  91. this.cacheIssuedTokens = value;
  92. }
  93. }
  94. public int IssuedTokenRenewalThresholdPercentage
  95. {
  96. get
  97. {
  98. return this.issuedTokenRenewalThresholdPercentage;
  99. }
  100. set
  101. {
  102. ThrowIfImmutable();
  103. this.issuedTokenRenewalThresholdPercentage = value;
  104. }
  105. }
  106. public Dictionary<Uri, KeyedByTypeCollection<IEndpointBehavior>> IssuerChannelBehaviors
  107. {
  108. get
  109. {
  110. if (this.issuerChannelBehaviors == null)
  111. this.issuerChannelBehaviors = new Dictionary<Uri, KeyedByTypeCollection<IEndpointBehavior>>();
  112. return this.issuerChannelBehaviors;
  113. }
  114. }
  115. public KeyedByTypeCollection<IEndpointBehavior> LocalIssuerChannelBehaviors
  116. {
  117. get
  118. {
  119. if (this.localIssuerChannelBehaviors == null)
  120. this.localIssuerChannelBehaviors = new KeyedByTypeCollection<IEndpointBehavior>();
  121. return this.localIssuerChannelBehaviors;
  122. }
  123. }
  124. public TimeSpan MaxIssuedTokenCachingTime
  125. {
  126. get
  127. {
  128. return this.maxIssuedTokenCachingTime;
  129. }
  130. set
  131. {
  132. if (value < TimeSpan.Zero)
  133. {
  134. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  135. SR.GetString(SR.SFxTimeoutOutOfRange0)));
  136. }
  137. if (TimeoutHelper.IsTooLarge(value))
  138. {
  139. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  140. SR.GetString(SR.SFxTimeoutOutOfRangeTooBig)));
  141. }
  142. ThrowIfImmutable();
  143. this.maxIssuedTokenCachingTime = value;
  144. }
  145. }
  146. KeyedByTypeCollection<IEndpointBehavior> GetBehaviorCollection(KeyedByTypeCollection<IEndpointBehavior> behaviors)
  147. {
  148. KeyedByTypeCollection<IEndpointBehavior> result = new KeyedByTypeCollection<IEndpointBehavior>();
  149. foreach (IEndpointBehavior behavior in behaviors)
  150. {
  151. result.Add(behavior);
  152. }
  153. return result;
  154. }
  155. internal void MakeReadOnly()
  156. {
  157. this.isReadOnly = true;
  158. }
  159. void ThrowIfImmutable()
  160. {
  161. if (this.isReadOnly)
  162. {
  163. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  164. }
  165. }
  166. }
  167. }