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