private static void InvalidateUserCredentials(string username) { if (CredentialCache.ContainsKey(username)) { CredentialCache.Remove(username); } DeleteDatabaseCredentials(ProtectUsername(username)); }
/// <summary> /// Create new account, using Person entity, password, and optionally privilegies level. /// </summary> /// <param name="person"></param> /// <param name="password"></param> /// <param name="privilegies"></param> public static void CreateAccount(Person person, string password, char privilegies = 'c') { using (var tx = Client.GetTransactions().TxStart()) { // Referential integrity check if (CredentialCache.ContainsKey(person.Phone)) { tx.Commit(); throw new ReferentialException("Can not create new account.") { ReadableMessage = $"Can not create account because account with phone {person.Phone} already exists." }; } if (!LocationCache.ContainsKey(person.LocationID)) { tx.Commit(); throw new ReferentialException("Can not create new account.") { ReadableMessage = $"Can not create account because location with key {person.LocationID} does not exist." }; } // Normal operation int key = LastUsedKeys.Get("person"); PersonCache.Put(key, person); LastUsedKeys.Put("person", key + 1); Credential c = new Credential { Password = password, Privilegies = (byte)privilegies, Status = (byte)'n', PersonID = key }; CredentialCache.Put(person.Phone, c); tx.Commit(); } }
/// <summary> /// Put a person into the cache.<br/> /// This method checks referential integrity on field Person.LocationID: <br/> /// Location with _key = Person.LocationID has to exist. <br/> /// Otherwise, method throws ReferentialException. <br/> /// This and other put methods DO NOT validate the entity. /// </summary> /// <param name="p">Person to put into the cache.</param> public static void PutPerson (Person p) { using (var tx = Client.GetTransactions().TxStart()) { // Check if Location cache contains Location with key p.LocationID, throw error if not. if (!(LocationCache.ContainsKey(p.LocationID))) { tx.Commit(); throw new ReferentialException ("Can not put new entry into Person cache.") { Operation = "put", TableName = "Person", FieldName = "LocationID", ReadableMessage = $"Can not put new entry into Person cache because Location with key {p.LocationID} does not exist." }; } // Check if Credential cache contains Credential with key p.Phone, throw error if yes. if (CredentialCache.ContainsKey(p.Phone)) { tx.Commit(); throw new ReferentialException ("Can not put new entry into Person cache.") { Operation = "put", TableName = "Person", FieldName = "Phone", ReadableMessage = $"Can not put new entry into Person cache because Person with Phone '{p.Phone}' and Credential with the same key already exist." }; } // Normal operation. int key = LastUsedKeys.Get("person"); PersonCache.Put(key, p); key++; LastUsedKeys.Put ("person", key); tx.Commit(); } }
public static SecureString GetUserCredential(string username) { if (CredentialCache.ContainsKey(username)) { return(CredentialCache[username]); } string obusername = ProtectUsername(username); byte[] enccred = GetDatabaseCredentials(obusername); if (enccred != null) { try { byte[] cred = ProtectedData.Unprotect(enccred, null, DataProtectionScope.LocalMachine); if (cred != null) { SecureString secstr = new SecureString(); foreach (byte c in cred) { secstr.AppendChar((char)c); } secstr.MakeReadOnly(); return(secstr); } } catch (CryptographicException) { InvalidateUserCredentials(username); } } return(null); }