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