public static bool RemoveApiKeysForUserID(int uid) { if (uid > 0) { // get all keys related to uid List <string> keysToDelete = new List <string>(); foreach (AuthTokens at in RepoFactory.AuthTokens.GetAll()) { if (at.UserID == uid) { keysToDelete.Add(at.Token); } } // remove keys from database if (keysToDelete.Count > 0) { foreach (string key in keysToDelete) { RemoveApiKey(key); } // get new user data UserDatabase.Refresh(); } return(true); } else { return(false); } }
public static string ValidateUser(string username, string password, string device) { //in case of login before database have been loaded if (Users.Count == 0) { UserDatabase.Refresh(); } var userRecord = Users.FirstOrDefault(u => u.Item2.Equals(username, StringComparison.OrdinalIgnoreCase) && u.Item3 == password); if (userRecord == null) { //if user is invalid try to refresh cache so we add any newly added users to cache just in case UserDatabase.Refresh(); return(null); } int uid = new SVR_JMMUser(username).JMMUserID; string apiKey = ""; try { var apiKeys = ActiveApiKeys.First(u => u.Item1 == uid && u.Item2 == device.ToLower()); apiKey = apiKeys.Item3; } catch { apiKey = Guid.NewGuid().ToString(); ActiveApiKeys.Add(new Tuple <int, string, string>(uid, device.ToLower(), apiKey)); AuthTokens token = new AuthTokens { UserID = uid, DeviceName = (device).ToLower(), Token = apiKey }; RepoFactory.AuthTokens.Save(token); } return(apiKey); }