//[ThreadStatic] //private static User CurraaentUser; #endregion #region Login public static string GetLoginData(LoginDataCriteria LoginData) { // IEnumerable<string> ReturnString=new string[]{}; using (UsersContext Context = new UsersContext()) { var text = "amer"; string DcriptedPassword = LoginData. Password; if (LoginData.IsEncrypted) DcriptedPassword = UserService.DecryptText(LoginData.Password, text); User CurrentUser; if (LoginData.IsAccout) CurrentUser = GetCurrentUser(LoginData.Email, DcriptedPassword); else CurrentUser = GetCurrentUser(LoginData.Email); // check if the login with username and password match if (CurrentUser == null) return "Login Faild"; var CurrentAccount = (from Acc in Context.Accounts where Acc.UserID == CurrentUser.UserID select Acc).FirstOrDefault(); var DTO = new UserLoginDTO() { FirstName = CurrentAccount.FirstName != null ? CurrentAccount.FirstName : "" , MiddelName = CurrentAccount.MiddelName != null ? CurrentAccount.MiddelName : "" , LastName = CurrentAccount.LastName != null ? CurrentAccount.LastName : "", Gender = CurrentAccount.Gender.HasValue ? CurrentAccount.Gender.Value.ToString() : "" , Email = CurrentUser.Email, Password = CurrentUser.Password != null ? CurrentUser.Password : "", CreatedOnDate = CurrentUser.CreatedOnDate.ToString("dd/MM/yyyy"), BirthDay = CurrentAccount.BirthDay.HasValue ? CurrentAccount.BirthDay.Value.ToString("dd/MM/yyyy") : "", UserID = CurrentUser.UserID, AccountID = CurrentAccount.AccountID, BadgeID = CurrentAccount.BadgeID != null ? CurrentAccount.BadgeID : 0, CountryID = CurrentAccount.CountryID != null ? CurrentAccount.CountryID : "", ModefiedOnDate = CurrentAccount.ModefiedOnDate.HasValue ? CurrentAccount.ModefiedOnDate.Value.ToString("dd/MM/yyyy") : "" , IsAutoLogin = LoginData.HeaderIsAutoLogin != null ? LoginData.HeaderIsAutoLogin.ToString() : "" , Points = CurrentAccount.Points.HasValue ? CurrentAccount.Points.Value : 0 }; return JsonConvert.SerializeObject(DTO); } }
public static User GetCurrentUser(string Email,string Password) { using (UsersContext Context = new UsersContext()) { CurrentUser = new User(); //... etc CurrentUser = (from b in Context.Users where b.Email == Email && b.Password==Password select b).FirstOrDefault(); } return CurrentUser; }
public static CheckTokenResult CheckTokenForSocialMediaLogin(DeviceTokenEntity DeviceTokenEntity, string SentEmail) { DeviceTokenEntity CurrentToken = new DeviceTokenEntity(); User CurrentUser = new User(); using (var Context = new UsersContext()) { if (Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken).Any()) { CurrentToken = Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken).FirstOrDefault(); var EmailCurrentToken = ""; if (CurrentToken != null) { var deviceUser = (from acc in Context.Accounts where CurrentToken.AccountID == acc.AccountID select acc.UserID).FirstOrDefault(); EmailCurrentToken = (from Usr in Context.Users where Usr.UserID == deviceUser select Usr.Email).FirstOrDefault(); } if (EmailCurrentToken == SentEmail) { CurrentUser = GetCurrentUser(SentEmail); if (CurrentUser != null) return CheckTokenResult.OK; else return CheckTokenResult.Register; } else { if (CurrentToken.DidChangeToday) return CheckTokenResult.ErrorDidChangeToday; else { CurrentUser = GetCurrentUser(SentEmail); if (CurrentUser != null) { CurrentToken.DidChangeToday = true; Context.Tokens.Attach(CurrentToken); var entry = Context.Entry(CurrentToken); entry.Property(e => e.DidChangeToday).IsModified = true; Context.SaveChanges(); return CheckTokenResult.OK; } else { return CheckTokenResult.Register; } } } }else { CurrentUser = GetCurrentUser(SentEmail); if (CurrentUser == null) return CheckTokenResult.Register; else { Account CurrentAccount = Context.Accounts.Where(p => p.UserID == CurrentUser.UserID).FirstOrDefault(); var NewToken = UserService.SaveNewToken(CurrentAccount.AccountID, DeviceTokenEntity.DeviceToken); var newTokenEntity = new DeviceTokenEntity() { AccountID = CurrentAccount.AccountID, DeviceToken = NewToken, DeviceEmail = null }; return CheckTokenResult.OK; } } } }
public static CheckTokenResult CheckTokenForLogin(DeviceTokenEntity DeviceTokenEntity, string SentEmail,string Password ) { using (var Context = new UsersContext()) { DeviceTokenEntity CurrentToken = new DeviceTokenEntity(); User CurrentUser = new User(); if (Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken).Any()) { CurrentToken = Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken).FirstOrDefault(); var EmailCurrentToken = ""; if (CurrentToken != null) { var deviceUser = (from acc in Context.Accounts where CurrentToken.AccountID == acc.AccountID select acc.UserID).FirstOrDefault(); EmailCurrentToken = (from Usr in Context.Users where Usr.UserID == deviceUser select Usr.Email).FirstOrDefault(); } if (EmailCurrentToken == SentEmail) { // if(!string.IsNullOrEmpty(Password)) CurrentUser = GetCurrentUser(SentEmail,Password); // else // CurrentUser = GetCurrentUser(SentEmail);// social media login if (CurrentUser != null) return CheckTokenResult.OK; else return CheckTokenResult.ErrorInvalidPassword; } else { if (CurrentToken.DidChangeToday) return CheckTokenResult.ErrorDidChangeToday; else { if (!string.IsNullOrEmpty(Password)) CurrentUser = GetCurrentUser(SentEmail, Password); else CurrentUser = GetCurrentUser(SentEmail); if(CurrentUser!=null) { CurrentToken.DidChangeToday = true; Context.Tokens.Attach(CurrentToken); var entry = Context.Entry(CurrentToken); entry.Property(e => e.DidChangeToday).IsModified = true; Context.SaveChanges(); return CheckTokenResult.OK; } else { return CheckTokenResult.ErrorInvalidPassword; } } } } else { CurrentUser = GetCurrentUser(SentEmail); if (CurrentUser == null) return CheckTokenResult.UserDoesntExist; else { CurrentUser = GetCurrentUser(SentEmail, Password); if (CurrentUser == null) return CheckTokenResult.ErrorInvalidPassword; else { Account CurrentAccount = Context.Accounts.Where(p => p.UserID == CurrentUser.UserID).FirstOrDefault(); var NewToken = UserService.SaveNewToken(CurrentAccount.AccountID, DeviceTokenEntity.DeviceToken); var newTokenEntity = new DeviceTokenEntity() { AccountID = CurrentAccount.AccountID, DeviceToken = NewToken, DeviceEmail = null }; return CheckTokenResult.OK; } } } #pragma warning disable CS0162 // Unreachable code detected return CheckTokenResult.None; #pragma warning restore CS0162 // Unreachable code detected } }