public Tuple <bool, string> AddOrUpdateAuthentication(AuthenticationModel authenticationModel, StateOperation stateOperation) { try { if (stateOperation == StateOperation.درج) { var authentication = _authenticationRepository.Find(x => x.IdentityCode == authenticationModel.IdentityCode); if (authentication != null) { return(new Tuple <bool, string>(false, "خطا در انجام عملیات : کد شناسایی مورد نظر تکراری می باشد")); } Authentication newAuthentication = new Authentication { AuthenticationType = authenticationModel.AuthenticationType, IdentityCode = authenticationModel.IdentityCode, CentralOrganizationId = authenticationModel.CentralOrganizationId, BranchProvinceId = authenticationModel.BranchProvinceId, UniversityId = authenticationModel.UniversityId }; _authenticationRepository.Add(newAuthentication); } else { if (_authenticationRepository.Contains(x => x.Id != authenticationModel.Id && x.IdentityCode == authenticationModel.IdentityCode)) { return(new Tuple <bool, string>(false, "خطا در انجام عملیات : کد شناسایی مورد نظر تکراری می باشد")); } var authentication = _authenticationRepository.Find(x => x.Id == authenticationModel.Id); if (authentication == null) { return(new Tuple <bool, string>(false, "خطا در انجام عملیات : رکورد مورد نظر یافت نشد")); } authentication.IdentityCode = authenticationModel.IdentityCode; authentication.AuthenticationType = authenticationModel.AuthenticationType; //authentication.CentralOrganizationId = authenticationModel.CentralOrganizationId; //authentication.BranchProvinceId = authenticationModel.BranchProvinceId; //authentication.UniversityId = authenticationModel.UniversityId; _authenticationRepository.Update(authentication); } _unitOfWork.SaveChanges(); return(new Tuple <bool, string>(true, "عملیات ثبت شد")); } catch (Exception ex) { return(new Tuple <bool, string>(false, "خطا در انجام عملیات")); } }
public void Handle(LoginCommand command) { User user = _authRepository.FindUserByUserName(command.Username); if (string.IsNullOrEmpty(command.Password) || string.IsNullOrEmpty(command.Username)) { _authRepository.Add(LoginAttempt.Failed(command.AttemptId, "Invalid credentials")); return; } if (user == null) { _authRepository.Add(LoginAttempt.Failed(command.AttemptId, "User not found")); return; } if (!_hasher.CompareHash(command.Password, user.Password, user.PasswordSalt)) { _authRepository.Add(LoginAttempt.Failed(command.AttemptId, "Invalid password", user)); return; } _authRepository.Add(LoginAttempt.Successful(command.AttemptId, user)); }
public async Task <User> Register(UserDTO user) { if (await UserExists(user.Username)) { return(null); } CreatePasswordHash(user.Password, out byte[] passwordHash, out byte[] passwordSalt); var userToReturn = new User() { PasswordHash = passwordHash, PasswordSalt = passwordSalt }; _repo.Add <User>(userToReturn); await _repo.SaveAllChanges(); return(userToReturn); }
/// <summary> /// این متد رکورد مربوط به کاربر مدیر را در سطوح مختلف /// به همراه احراز هویت ، نقش ، شخص ، پروفایل و انتساب نقش به یوزر وارد می کند /// </summary> /// <param name="levelId"></param> /// <param name="authenticationType"></param> /// <param name="roleType"></param> /// <param name="baseRegisterProgramModel"></param> /// <returns></returns> public virtual bool AddUserAdminAutomatic(long levelId, AuthenticationType authenticationType, RoleType roleType, BaseRegisterProgramModel baseRegisterProgramModel) { try { var hasher = new PasswordHasher(); //ثبت رکورد احراز هویت برای مدیر دانشگاه Authentication authentication = new Authentication(levelId, authenticationType, baseRegisterProgramModel.LicenceCode); _authenticationRepository.Add(authentication); //ثبت رکورد نقش مدیر دانشگاه Role role = new Role(levelId, roleType); _roleRepository.Add(role); //ایجاد کاربر برای مدیر دانشگاه User user = new User { AuthenticationId = authentication.Id, Email = baseRegisterProgramModel.UserName, UserName = baseRegisterProgramModel.UserName, PasswordHash = hasher.HashPassword(baseRegisterProgramModel.Password), SecurityStamp = Guid.NewGuid().ToString(), EmailConfirmed = true }; //ایجاد پروفایل Profile profile = new Profile { Name = baseRegisterProgramModel.Name, Family = baseRegisterProgramModel.Family, NationalCode = baseRegisterProgramModel.NationalCode, Mobile = baseRegisterProgramModel.Mobile }; // ثبت رکورد جدول شخص و کاربر برای مدیر دانشگاه Person person = new Person { User = user, Profile = profile }; if (authenticationType == AuthenticationType.AdminCentral) { person.CentralOrganizationId = levelId; } else if (authenticationType == AuthenticationType.AdminBranch) { person.BranchProvinceId = levelId; } else { person.UniversityId = levelId; } _personRepository.Add(person); //انتساب نقش مدیریت به کاربر دانشگاه UserRole userRole = new UserRole { RoleId = role.Id, UserId = person.Id }; _userRoleRepository.Add(userRole); _unitOfWork.SaveChanges(); return(true); } catch (Exception ex) { return(false); } }