Example #1
0
        private void InvalidateAuthenticationToken(HttpRequestMessage request)
        {
            // Get the authentication token provided by the client in the request message
            string authenticationToken = GetAuthenticationTokenFromCookie(request, AuthenticationToken);

            if ((object)authenticationToken == null)
            {
                return;
            }

            string selector = authenticationToken.Split(':')[0];

            // Determine where the credential cache is located
            ConfigurationFile configFile = ConfigurationFile.Current;
            CategorizedSettingsElementCollection systemSettings = configFile.Settings["systemSettings"];
            string configurationCachePath = systemSettings["ConfigurationCachePath"].Value;
            string credentialCachePath    = Path.Combine(configurationCachePath, "CredentialCache.bin");

            // Open the credential cache
            lock (s_credentialCacheLock)
            {
                using (FileBackedDictionary <string, Credential> credentialCache = new FileBackedDictionary <string, Credential>(credentialCachePath))
                {
                    credentialCache.Remove(selector);
                }
            }
        }
Example #2
0
 public void RemoveTest()
 {
     using (FileBackedDictionary<int, int> dictionary = new FileBackedDictionary<int, int>())
     {
         dictionary.Add(0, 0);
         Assert.IsTrue(dictionary.ContainsKey(0));
         dictionary.Remove(0);
         Assert.IsFalse(dictionary.ContainsKey(0));
         Assert.AreEqual(dictionary.Count, 0);
     }
 }
Example #3
0
 public void RemoveTest()
 {
     using (FileBackedDictionary <int, int> dictionary = new FileBackedDictionary <int, int>())
     {
         dictionary.Add(0, 0);
         Assert.IsTrue(dictionary.ContainsKey(0));
         dictionary.Remove(0);
         Assert.IsFalse(dictionary.ContainsKey(0));
         Assert.AreEqual(dictionary.Count, 0);
     }
 }
Example #4
0
        private string IssueAuthenticationToken(string username, string password)
        {
            byte[] buffer = new byte[9];

            // Generate the selector for the token
            Random.GetBytes(buffer);
            string selector = Convert.ToBase64String(buffer);

            // Generate the validator for the token
            Random.GetBytes(buffer);
            string validator = Convert.ToBase64String(buffer);

            // Determine where the credential cache is located
            ConfigurationFile configFile = ConfigurationFile.Current;
            CategorizedSettingsElementCollection systemSettings = configFile.Settings["systemSettings"];

            string configurationCachePath = systemSettings["ConfigurationCachePath"].Value;
            string credentialCachePath    = Path.Combine(configurationCachePath, "CredentialCache.bin");

            // Open the credential cache
            lock (s_credentialCacheLock)
            {
                using (FileBackedDictionary <string, Credential> credentialCache = new FileBackedDictionary <string, Credential>(credentialCachePath))
                {
                    // Clean out expired credentials before issuing a new one
                    DateTime now = DateTime.UtcNow;

                    List <string> expiredSelectors = credentialCache
                                                     .Where(kvp => now >= kvp.Value.Expiration)
                                                     .Select(kvp => kvp.Key)
                                                     .ToList();

                    foreach (string expiredSelector in expiredSelectors)
                    {
                        credentialCache.Remove(expiredSelector);
                    }

                    credentialCache.Compact();

                    // Enter the new token into the credential cache
                    credentialCache[selector] = new Credential
                    {
                        Validator  = validator,
                        Username   = username,
                        Password   = password,
                        Expiration = DateTime.UtcNow.AddDays(30.0D)
                    };
                }
            }

            return($"{selector}:{validator}");
        }
Example #5
0
        public void CompactTest()
        {
            using (FileBackedDictionary <int, int> dictionary = new FileBackedDictionary <int, int>())
            {
                for (int i = 0; i < 10000; i += 4)
                {
                    dictionary.Add(i, 4);

                    if (i % 400 == 0)
                    {
                        dictionary[i] = 400;
                    }
                    else if (i % 100 == 0)
                    {
                        dictionary.Remove(i);
                    }
                }

                dictionary.Compact();

                for (int i = 0; i < 10000; i++)
                {
                    if (i % 400 == 0)
                    {
                        Assert.AreEqual(dictionary[i], 400);
                    }
                    else if (i % 100 == 0)
                    {
                        Assert.IsFalse(dictionary.ContainsKey(i), i.ToString());
                    }
                    else if (i % 4 == 0)
                    {
                        Assert.AreEqual(dictionary[i], 4);
                    }
                    else
                    {
                        Assert.IsFalse(dictionary.ContainsKey(i), i.ToString());
                    }
                }
            }
        }
Example #6
0
        public void CompactTest()
        {
            using (FileBackedDictionary<int, int> dictionary = new FileBackedDictionary<int, int>())
            {
                for (int i = 0; i < 10000; i += 4)
                {
                    dictionary.Add(i, 4);

                    if (i % 400 == 0)
                        dictionary[i] = 400;
                    else if (i % 100 == 0)
                        dictionary.Remove(i);
                }

                dictionary.Compact();

                for (int i = 0; i < 10000; i++)
                {
                    if (i % 400 == 0)
                        Assert.AreEqual(dictionary[i], 400);
                    else if (i % 100 == 0)
                        Assert.IsFalse(dictionary.ContainsKey(i), i.ToString());
                    else if (i % 4 ==  0)
                        Assert.AreEqual(dictionary[i], 4);
                    else
                        Assert.IsFalse(dictionary.ContainsKey(i), i.ToString());
                }
            }
        }