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