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}"); }