public static void MatchIdentity(CSSDataContext db, Login login, MachineInformation info, out Identity principal, out bool wasMerged) { principal = login.Identity; wasMerged = false; var machineRecords = login.Identity.Logins.SelectMany(p => p.MachineRecords); foreach (var deviceInfo in info.MachineValues) { var existingMachineRecord = machineRecords.FirstOrDefault( p => p.RecordTypeId == (int)deviceInfo.Type && p.Identifier == deviceInfo.Value); if (existingMachineRecord == null) { login.MachineRecords.Add(new MachineRecord() { RecordTypeId = (int)deviceInfo.Type, Identifier = deviceInfo.Value, Login = login, LoginId = login.Id }); } } // Find matching machine records for other logins. List <MatchedMachineRecord> matchedMachineRecords = new List <MatchedMachineRecord>(); List <Identity> matchingIdentities = new List <Identity>(); matchingIdentities.Add(login.Identity); // Only match on hard drive serial numbers for now. var machineRecordsAttachedToLogin = login.Identity.Logins.SelectMany(p => p.MachineRecords).Where(p => p.DeviceType == DeviceType.HardDisk); var machineRecordsByIdentity = MachineRecordByIdentity.GetAllFromCache(); foreach (var machineRecord in machineRecordsAttachedToLogin) { if (MachineRecordExclusion.IsMachineRecordExcluded(machineRecord) == true) { continue; } //var matchingMachineRecords = db.MachineRecords // .Where(p => p.Login.Identity != login.Identity // && p.RecordTypeId == machineRecord.RecordTypeId // && p.Identifier == machineRecord.Identifier) // .ToList() // Release from the database context so that AreIdentitiesPermanentlyUnlinked can be evaluated. // .Where(p => AreIdentitiesPermanentlyUnlinked(db, p.Login.Identity, login.Identity) == false); var matchingMachineRecords = machineRecordsByIdentity .Where(p => p.IdentityId != login.IdentityId && p.RecordTypeId == machineRecord.RecordTypeId && p.Identifier == machineRecord.Identifier) .ToList() // Release from the database context so that AreIdentitiesPermanentlyUnlinked can be evaluated. .Where(p => AreIdentitiesPermanentlyUnlinked(db, p.IdentityId, login.IdentityId) == false); //matchedMachineRecords = matchingMachineRecords.Select(p => new MatchedMachineRecord() //{ // machineRecord1 = machineRecord, // machineRecord2 = p //}).ToList(); matchingIdentities.AddRange( db.Identities .Where(p => matchingMachineRecords.Select(r => r.IdentityId).Contains(p.Id)) .ToList() ); //matchingIdentities.AddRange(matchingMachineRecords.Select(p => p.Login.Identity).ToList()); //var matchingMachineRecords = db.MachineRecords // .Where( p => p.RecordTypeId == machineRecord.RecordTypeId // && p.Identifier == machineRecord.Identifier // && machineRecordsAttachedToLogin.Count(r => r.Identifier == p.Identifier && r.MachineRecordType == p.MachineRecordType) == 0); //foreach (var matchingMachineRecord in matchingMachineRecords) //{ // if (matchingIdentities.Contains(matchingMachineRecord.Login.Identity) == false) // { // matchingIdentities.Add(matchingMachineRecord.Login.Identity); // matchedMachineRecords.Add(new MatchedMachineRecord() // { // machineRecord1 = machineRecord, // machineRecord2 = matchingMachineRecord // }); // } //} } db.SubmitChanges(); // If a matching machine record was found for a login that is not already linked to the principal identity, // then link the login to the principal identity. if (matchingIdentities.Count > 1) { List <string> accounts = new List <string>(); foreach (var joinedIdentity in matchingIdentities) { accounts.Add("[" + String.Join(",", joinedIdentity.Logins.Select(p => p.Username).ToArray()) + "]"); } List <string> machineRecordMatches = new List <string>(); foreach (var matchedMachineRecord in matchedMachineRecords) { machineRecordMatches.Add(String.Format("Login: {0}, Id: {1}, Type: {2}, Value: {3} == Login: {4}, Id: {5}, Type: {6}, Value: {7}", matchedMachineRecord.machineRecord1.LoginId, matchedMachineRecord.machineRecord1.Id, matchedMachineRecord.machineRecord1.RecordTypeId, matchedMachineRecord.machineRecord1.Identifier, matchedMachineRecord.machineRecord2.LoginId, matchedMachineRecord.machineRecord2.Id, matchedMachineRecord.machineRecord2.RecordTypeId, matchedMachineRecord.machineRecord2.Identifier)); } principal = MergeIdentities(db, matchingIdentities); wasMerged = true; Log.Write(LogType.AuthenticationServer, "Accounts Merged by V3 Automation: " + String.Join(",", accounts.ToArray()) + " -- Machine Record Matches: " + String.Join(" -- ", machineRecordMatches.ToArray())); db.SubmitChanges(); } }