public IEnumerable<string> Get() { UsersContext UserCon=new UsersContext(); User myUser=UserCon.Users.FirstOrDefault(); return new string[] { myUser.Email,myUser.Password}; }
//[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 string ChangePassword(string UserEmail, string OldPassword, string NewPassword) { User currentUser = GetCurrentUser(UserEmail,OldPassword); if (currentUser != null) { using (UsersContext Context = new UsersContext()) { currentUser.Password = NewPassword; Context.Users.Attach(currentUser); var entry = Context.Entry(currentUser); entry.Property(e => e.Password).IsModified = true; Context.SaveChanges(); return currentUser.Password; } } else { return "Error Old password Not Correct"; } }
public static string ResetPassword(string UserEmail) { User currentUser = GetCurrentUser(UserEmail); using (UsersContext Context = new UsersContext()) { if (currentUser != null) { currentUser.Password = GenerateRandomPasswordOrName(10); Context.Users.Attach(currentUser); var entry = Context.Entry(currentUser); entry.Property(e => e.Password).IsModified = true; Context.SaveChanges(); return currentUser.Password; } return "user not registerd"; } }
public static string SaveNewUser(string UserEmail, string Password, string DeviceToken) { try { using (var context = new UsersContext()) { // add new User var NewUser = context.Users.Add(new User() { Email = UserEmail, Password = Password, CreatedOnDate = DateTime.Now }); context.SaveChanges(); //then add new account var NewAccount = context.Accounts.Add(new Account() { UserID = NewUser.Entity.UserID, CreatedOnDate = DateTime.Now, ModefiedOnDate = null }); context.SaveChanges(); //finally add new device token ....... var newToken = context.Tokens.Add(new DeviceTokenEntity() { DeviceToken = DeviceToken, DidChangeToday = false, IsActive = true, AccountID = NewAccount.Entity.AccountID }); context.SaveChanges(); LoginDataCriteria LoginData = new LoginDataCriteria() { Email = UserEmail, Password = Password, HeaderIsAutoLogin = null, IsEncrypted = false }; if (Password == null) LoginData.IsAccout = false; return UserService.GetLoginData(LoginData); } } catch (Exception ex) { return ex.Message; } }
public static DeviceTokenEntity CheckTokenForRegister(DeviceTokenEntity DeviceTokenEntity, string SentEmail) { using (var Context = new UsersContext()) { DeviceTokenEntity CurrentToken = new DeviceTokenEntity(); User CurrentUser = GetCurrentUser(SentEmail); if (CurrentUser != null) { Account CurrentAccount = Context.Accounts.Where(p => p.UserID == CurrentUser.UserID).FirstOrDefault(); // CurrentToken = Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken && p.AccountID == CurrentAccount.AccountID).Any(); if (Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken && p.AccountID == CurrentAccount.AccountID).Any()) { CurrentToken = Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken && p.AccountID == CurrentAccount.AccountID).FirstOrDefault(); CurrentToken.DeviceEmail = null; return CurrentToken; } else // Existed Account login with new Device { var NewToken = UserService.SaveNewToken(CurrentAccount.AccountID, DeviceTokenEntity.DeviceToken); var newTokenEntity = new DeviceTokenEntity() { AccountID = CurrentAccount.AccountID, DeviceToken = NewToken, DeviceEmail = null }; return newTokenEntity; } } else { CurrentToken = Context.Tokens.Where(p => p.DeviceToken == DeviceTokenEntity.DeviceToken).FirstOrDefault(); if (CurrentToken != null) { var deviceUser = (from acc in Context.Accounts where CurrentToken.AccountID == acc.AccountID select acc.UserID).FirstOrDefault(); CurrentToken.DeviceEmail = (from Usr in Context.Users where Usr.UserID == deviceUser select Usr.Email).FirstOrDefault(); return CurrentToken; } return null; } return CurrentToken; } }
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 } }
public static string SaveNewToken(int AccountID,string DeviceToken) { using (var context = new UsersContext()) { var newToken = context.Tokens.Add(new DeviceTokenEntity() { DeviceToken = DeviceToken, DidChangeToday = false, IsActive = true, AccountID =AccountID }); context.SaveChanges(); return DeviceToken; } }