public Authentication CheckAuthenticationFirstAccess(Guid integrationCode) { var _context = _contextFactory(); var email = (from u in _context.User where u.IntegrationCode.Equals(integrationCode) select new { u.Email }).FirstOrDefault(); var userPerson = (from u in _context.User join p in _context.Person on u.PersonId equals p.PersonId //where u.IntegrationCode.Equals(integrationCode) where u.Email.Equals(email.Email) && u.Active && (u.UserStatusId == (byte)GeneralEnumerators.EnumUserStatus.Ativo || u.UserStatusId == (byte)GeneralEnumerators.EnumUserStatus.AguardandoAtivacao) select new { u.UserId, u.Email, u.PersonId, u.Name, u.UserProfileId, u.UrlImagemLogo, p.PersonRules, p.PersonTypeId, p.IntegrationCode, UserIntegrationCode = u.IntegrationCode, Complete = string.IsNullOrEmpty(u.SmartPhoneNumber) ? 0 : 1 }).ToList(); if (userPerson.Count == 0) { return(null); } var auth = userPerson.Select(x => new Authentication { UserId = x.UserId, Email = x.Email, Name = x.Name, PersonId = x.PersonId, PersonTypeId = x.PersonTypeId, PersonRules = x.PersonRules, UserProfileId = x.UserProfileId, PersonIntegrationCode = x.IntegrationCode, UserIntegrationCode = x.UserIntegrationCode, Complete = x.Complete, ImgProfileLogo = x.UrlImagemLogo }).ToList(); auth.ForEach(x => x.ProfileClaims = CustomProfile.ListProfiles(x.UserProfileId, x.PersonRules.Select(y => y.PersonProfileId).ToList())); List <int> managerProfiles = Constants.DefaultManagerAccessProfile(); Authentication user = null; foreach (var item in auth) { foreach (var profileClaim in item.ProfileClaims) { if (managerProfiles.Contains(profileClaim)) { return(item); // in case of manager } } } // else return natural person return(auth.Where(x => x.PersonTypeId == (int)EnumPersonType.Natural_Person).First()); }