The Labs \ Source Viewer \ SSCLI \ System.Net.Security \ SSPIHandleCache

  1. //------------------------------------------------------------------------------
  2. // <copyright file="_SspiSessionCache.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*++
  16. Abstract:
  17.     The file implements trivial SSPI credential caching mechanism based on lru list
  18. Author:
  19.     Alexei Vopilov    20-Oct-2004
  20. Revision History:
  21. --*/
  22. namespace System.Net.Security
  23. {
  24.     using System.Net;
  25.     using System.Threading;
  26.     using System.Collections;
  27.    
  28.    
  29.     //
  30.     // Implements delayed SSPI handle release, like a finalizable object though the handles are kept alive until being pushed out
  31.     // by the newly incoming ones.
  32.     //
  33.     static internal class SSPIHandleCache
  34.     {
  35.         private const int c_MaxCacheSize = 31;
  36.         // must a (power of 2) - 1
  37.         private static SafeCredentialReference[] _CacheSlots = new SafeCredentialReference[c_MaxCacheSize + 1];
  38.         private static int _Current = -1;
  39.        
  40.         static internal void CacheCredential(SafeFreeCredentials newHandle)
  41.         {
  42.             try {
  43.                 SafeCredentialReference newRef = SafeCredentialReference.CreateReference(newHandle);
  44.                 if (newRef == null)
  45.                     return;
  46.                 unchecked {
  47.                     int index = Interlocked.Increment(ref _Current) & c_MaxCacheSize;
  48.                     newRef = Interlocked.Exchange<SafeCredentialReference>(ref _CacheSlots[index], newRef);
  49.                 }
  50.                 if (newRef != null)
  51.                     newRef.Close();
  52.             }
  53.             catch (Exception e) {
  54.                 if (!NclUtilities.IsFatal(e)) {
  55.                     GlobalLog.Assert("SSPIHandlCache", "Attempted to throw: " + e.ToString());
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }

Developer Fusion