SecurityContextSecurityTokenResolver.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security.Tokens
  5. {
  6. using System.Xml;
  7. using System.ServiceModel;
  8. using System.Collections.ObjectModel;
  9. using System.IdentityModel.Selectors;
  10. using System.IdentityModel.Tokens;
  11. public class SecurityContextSecurityTokenResolver : SecurityTokenResolver, ISecurityContextSecurityTokenCache
  12. {
  13. SecurityContextTokenCache tokenCache;
  14. bool removeOldestTokensOnCacheFull;
  15. int capacity;
  16. TimeSpan clockSkew = SecurityProtocolFactory.defaultMaxClockSkew;
  17. public SecurityContextSecurityTokenResolver( int securityContextCacheCapacity, bool removeOldestTokensOnCacheFull )
  18. : this( securityContextCacheCapacity, removeOldestTokensOnCacheFull, SecurityProtocolFactory.defaultMaxClockSkew )
  19. {
  20. }
  21. public SecurityContextSecurityTokenResolver(int securityContextCacheCapacity, bool removeOldestTokensOnCacheFull, TimeSpan clockSkew)
  22. {
  23. if (securityContextCacheCapacity <= 0)
  24. {
  25. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("securityContextCacheCapacity", SR.GetString(SR.ValueMustBeGreaterThanZero)));
  26. }
  27. if ( clockSkew < TimeSpan.Zero )
  28. {
  29. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ArgumentOutOfRangeException( "clockSkew", SR.GetString( SR.TimeSpanCannotBeLessThanTimeSpanZero ) ) );
  30. }
  31. this.capacity = securityContextCacheCapacity;
  32. this.removeOldestTokensOnCacheFull = removeOldestTokensOnCacheFull;
  33. this.clockSkew = clockSkew;
  34. this.tokenCache = new SecurityContextTokenCache(this.capacity, this.removeOldestTokensOnCacheFull, clockSkew);
  35. }
  36. public int SecurityContextTokenCacheCapacity
  37. {
  38. get
  39. {
  40. return this.capacity;
  41. }
  42. }
  43. public TimeSpan ClockSkew
  44. {
  45. get
  46. {
  47. return this.clockSkew;
  48. }
  49. }
  50. public bool RemoveOldestTokensOnCacheFull
  51. {
  52. get
  53. {
  54. return this.removeOldestTokensOnCacheFull;
  55. }
  56. }
  57. public void AddContext(SecurityContextSecurityToken token)
  58. {
  59. this.tokenCache.AddContext(token);
  60. }
  61. public bool TryAddContext(SecurityContextSecurityToken token)
  62. {
  63. return this.tokenCache.TryAddContext(token);
  64. }
  65. public void ClearContexts()
  66. {
  67. this.tokenCache.ClearContexts();
  68. }
  69. public void RemoveContext(UniqueId contextId, UniqueId generation)
  70. {
  71. this.tokenCache.RemoveContext(contextId, generation, false);
  72. }
  73. public void RemoveAllContexts(UniqueId contextId)
  74. {
  75. this.tokenCache.RemoveAllContexts(contextId);
  76. }
  77. public SecurityContextSecurityToken GetContext(UniqueId contextId, UniqueId generation)
  78. {
  79. return this.tokenCache.GetContext(contextId, generation);
  80. }
  81. public Collection<SecurityContextSecurityToken> GetAllContexts(UniqueId contextId)
  82. {
  83. return this.tokenCache.GetAllContexts(contextId);
  84. }
  85. public void UpdateContextCachingTime(SecurityContextSecurityToken context, DateTime expirationTime)
  86. {
  87. this.tokenCache.UpdateContextCachingTime(context, expirationTime);
  88. }
  89. protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token)
  90. {
  91. SecurityContextKeyIdentifierClause sctSkiClause = keyIdentifierClause as SecurityContextKeyIdentifierClause;
  92. if (sctSkiClause != null)
  93. {
  94. token = this.tokenCache.GetContext(sctSkiClause.ContextId, sctSkiClause.Generation);
  95. }
  96. else
  97. {
  98. token = null;
  99. }
  100. return (token != null);
  101. }
  102. protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key)
  103. {
  104. SecurityToken sct;
  105. if (TryResolveTokenCore(keyIdentifierClause, out sct))
  106. {
  107. key = ((SecurityContextSecurityToken)sct).SecurityKeys[0];
  108. return true;
  109. }
  110. else
  111. {
  112. key = null;
  113. return false;
  114. }
  115. }
  116. protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token)
  117. {
  118. SecurityContextKeyIdentifierClause sctSkiClause;
  119. if (keyIdentifier.TryFind<SecurityContextKeyIdentifierClause>(out sctSkiClause))
  120. {
  121. return TryResolveToken(sctSkiClause, out token);
  122. }
  123. else
  124. {
  125. token = null;
  126. return false;
  127. }
  128. }
  129. }
  130. }