public bool UpdatePassword(string email, string password)
        {
            var dbAccount = Context.Set <AuthInternal>().FirstOrDefault(u => u.email == email);

            if (dbAccount != null)
            {
                var hashNewPsw = PasswordHashProvider.CreateHash(password);
                dbAccount.password = hashNewPsw;

                Context.SaveChanges();
                return(true);
            }

            return(false);
        }
예제 #2
0
        public async ValueTask <string> Login(string name, string password, DateTime expireTime)
        {
            var context     = Context;
            var userService = new UserService(context);
            var user        = await userService.GetSingleAsync(it => it.Name == name);

            if (user == null)
            {
                return(null);
            }

            password = PasswordHashProvider.CreateHash(user.Salt, password, 16);
            if (!EncryptUtils.ComparePassword(user.Password, password))
            {
                return(null);
            }

            var token = EncryptUtils.GenerateRandomString(32);

            return(await AddSession(token, user, expireTime, context) ? token : null);
        }
예제 #3
0
        public void Register(LoginRequest account)
        {
            var dbUser = _authInternalRepository.FindByEmail(account.Email);

            if (dbUser == null)
            {
                using (var transaction = Context.Database.BeginTransaction())
                {
                    try
                    {
                        _accountRepository.Create(new Account
                        {
                            first_name      = account.FirstName,
                            last_name       = account.LastName,
                            username        = account.UserName,
                            registration_ip = account.RegistrationIP
                        });
                        _authInternalRepository.Create(new AuthInternal
                        {
                            account_id = _accountRepository.GetByUserName(account.UserName).id,
                            email      = account.Email,
                            password   = PasswordHashProvider.CreateHash(account.Password)
                        });

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
            else
            {
                _logger.LogError($"Account with {account.Email} already exist!");
                throw new WebException(HttpStatusCode.BadRequest.ToString());
            }
        }
예제 #4
0
        public async ValueTask <RegisterResult> Register(string name, string password, string invitationCode)
        {
            var context = Context;

            try
            {
                context.BeginTran();

                var invitationService = new InvitationService(context);
                var invitation        = await invitationService.
                                        GetSingleAsync(it => it.InvitationCode == invitationCode);

                if (invitation == null)
                {
                    context.RollbackTran();
                    return(RegisterResult.InvitationCodeNotFound);
                }

                if (invitation.ReceiverId > 0)
                {
                    context.RollbackTran();
                    return(RegisterResult.InvitationCodeHasBeenUsed);
                }

                var salt = EncryptUtils.GenerateRandomBytes(64);
                password = PasswordHashProvider.CreateHash(salt, password, 16);
                var user = new User
                {
                    Id        = IdGenerator.CreateId(),
                    Name      = name,
                    Password  = password,
                    Salt      = salt,
                    Authority = invitation.Authority
                };
                var userService = new UserService(context);
                var result      = userService.Insert(user);
                if (result.ErrorList.Count != 0)
                {
                    context.RollbackTran();
                    return(RegisterResult.UsernameRepeated);
                }

                var success = await invitationService.UpdateAsync(
                    it => new Invitation()
                {
                    UsedTime = DateTime.UtcNow, ReceiverId = user.Id
                },
                    it => it.ObjectId == invitation.ObjectId);

                if (!success)
                {
                    context.RollbackTran();
                    return(RegisterResult.InvitationCodeHasBeenUsed);
                }

                context.CommitTran();
                return(RegisterResult.Success);
            }
            catch
            {
                context.RollbackTran();
                throw;
            }
        }