예제 #1
0
        public async Task <string> CheckWetherCanChangePassword(MyUserAdapterModel myUserAdapterModel, string newPassword)
        {
            string result = "";

            CleanTrackingHelper.Clean <AccountPolicy>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
            AccountPolicy AccountPolicy = await context.AccountPolicy
                                          .OrderBy(x => x.Id)
                                          .FirstOrDefaultAsync();

            string encodePassword = PasswordHelper.GetPasswordSHA(myUserAdapterModel.Salt, newPassword);

            if (encodePassword == myUserAdapterModel.Password)
            {
                result = "不可以變更成為現在正在使用的密碼";
            }
            else
            {
                if (AccountPolicy.EnablePasswordHistory)
                {
                    var history = await context.MyUserPasswordHistory
                                  .FirstOrDefaultAsync(x => x.MyUserId == myUserAdapterModel.Id &&
                                                       x.Password == encodePassword);

                    if (history != null)
                    {
                        result = "不可以變更成為之前用過的密碼";
                    }
                }
            }
            return(result);
        }
        private async Task 建立系統定義參數Async()
        {
            #region 建立系統定義參數

            CleanTrackingHelper.Clean <AccountPolicy>(context);
            #region 新增系統定義紀錄
            AccountPolicy AccountPolicy = new AccountPolicy()
            {
                EnableLoginFailDetection  = true,
                LoginFailMaxTimes         = 3,
                LoginFailTimesLockMinutes = 5,
                MinimumPasswordLength     = 3,
                PasswordAge            = 42,
                PasswordComplexity     = 3, // PasswordStrength.Medium
                PasswordHistory        = 20,
                EnableCheckPasswordAge = true,
            };

            context.AccountPolicy.Add(AccountPolicy);
            await context.SaveChangesAsync();

            CleanTrackingHelper.Clean <AccountPolicy>(context);
            #endregion
            #endregion
        }
        public async Task <VerifyRecordResult> DeleteAsync(int id)
        {
            try
            {
                CleanTrackingHelper.Clean <AccountPolicy>(context);
                AccountPolicy item = await context.AccountPolicy
                                     .AsNoTracking()
                                     .FirstOrDefaultAsync(x => x.Id == id);

                if (item == null)
                {
                    return(VerifyRecordResultFactory.Build(false, ErrorMessageEnum.無法刪除紀錄));
                }
                else
                {
                    CleanTrackingHelper.Clean <AccountPolicy>(context);
                    context.Entry(item).State = EntityState.Deleted;
                    await context.SaveChangesAsync();

                    CleanTrackingHelper.Clean <AccountPolicy>(context);
                    return(VerifyRecordResultFactory.Build(true));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "刪除記錄發生例外異常");
                return(VerifyRecordResultFactory.Build(false, "刪除記錄發生例外異常", ex));
            }
        }
        public async Task <VerifyRecordResult> UpdateAsync(AccountPolicyAdapterModel paraObject)
        {
            try
            {
                AccountPolicy itemData = Mapper.Map <AccountPolicy>(paraObject);
                CleanTrackingHelper.Clean <AccountPolicy>(context);
                AccountPolicy item = await context.AccountPolicy
                                     .AsNoTracking()
                                     .FirstOrDefaultAsync(x => x.Id == paraObject.Id);

                if (item == null)
                {
                    return(VerifyRecordResultFactory.Build(false, ErrorMessageEnum.無法修改紀錄));
                }
                else
                {
                    CleanTrackingHelper.Clean <AccountPolicy>(context);
                    context.Entry(itemData).State = EntityState.Modified;
                    await context.SaveChangesAsync();

                    CleanTrackingHelper.Clean <AccountPolicy>(context);
                    return(VerifyRecordResultFactory.Build(true));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "修改記錄發生例外異常");
                return(VerifyRecordResultFactory.Build(false, "修改記錄發生例外異常", ex));
            }
        }
        public async Task <AccountPolicyAdapterModel> GetAsync(int id)

        {
            AccountPolicy item = await context.AccountPolicy
                                 .AsNoTracking()
                                 .FirstOrDefaultAsync(x => x.Id == id);

            AccountPolicyAdapterModel result = Mapper.Map <AccountPolicyAdapterModel>(item);

            await OhterDependencyData(result);

            return(result);
        }
        public async Task <VerifyRecordResult> AddAsync(AccountPolicyAdapterModel paraObject)
        {
            try
            {
                AccountPolicy itemParameter = Mapper.Map <AccountPolicy>(paraObject);
                CleanTrackingHelper.Clean <AccountPolicy>(context);
                await context.AccountPolicy
                .AddAsync(itemParameter);

                await context.SaveChangesAsync();

                CleanTrackingHelper.Clean <AccountPolicy>(context);
                return(VerifyRecordResultFactory.Build(true));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "新增記錄發生例外異常");
                return(VerifyRecordResultFactory.Build(false, "新增記錄發生例外異常", ex));
            }
        }
        public async Task CheckPasswordAge(CancellationToken cancellationToken)
        {
            CleanTrackingHelper.Clean <AccountPolicy>(context);
            CleanTrackingHelper.Clean <MyUser>(context);

            AccountPolicy AccountPolicy = await context.AccountPolicy
                                          .OrderBy(x => x.Id)
                                          .FirstOrDefaultAsync();

            cancellationToken.ThrowIfCancellationRequested();
            List <MyUser> myUsers = await context.MyUser
                                    .ToListAsync();

            cancellationToken.ThrowIfCancellationRequested();

            var enableCheckPasswordAge = AccountPolicy.EnableCheckPasswordAge;
            var passwordAge            = AccountPolicy.PasswordAge;

            if (enableCheckPasswordAge == true)
            {
                foreach (var item in myUsers)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (DateTime.Now > item.ForceChangePasswordDatetime)
                    {
                        #region 該使用者已經達到要變更密碼的時間
                        item.ForceChangePasswordDatetime = DateTime.Now.AddDays(passwordAge);
                        item.ForceChangePassword         = true;
                        context.Update(item);
                        await context.SaveChangesAsync();

                        #endregion
                    }
                }
            }

            CleanTrackingHelper.Clean <MyUser>(context);
            CleanTrackingHelper.Clean <AccountPolicy>(context);
        }
예제 #8
0
        public void SaveAccountPolicy(AccountPolicyModel model)
        {
            try
            {
                AccountPolicy accPolicyPremium = new AccountPolicy();
                accPolicyPremium.CreatedAt           = DateTime.Now;
                accPolicyPremium.RecieptAndPaymentId = model.RecieptAndPaymentId;
                accPolicyPremium.PolicyId            = model.PolicyId;
                accPolicyPremium.PolicyNumber        = model.PolicyNumber;
                accPolicyPremium.AccountType         = (int)PolicyAccountType.Premium;
                accPolicyPremium.Amount      = model.Premium.Value;
                accPolicyPremium.AccountName = PolicyAccountType.Premium.ToString();
                accPolicyPremium.Status      = model.Status;
                InsuranceContext.AccountPolices.Insert(accPolicyPremium);


                AccountPolicy accPolicyStamp = new AccountPolicy();
                accPolicyStamp.CreatedAt           = DateTime.Now;
                accPolicyStamp.RecieptAndPaymentId = model.RecieptAndPaymentId;
                accPolicyStamp.PolicyId            = model.PolicyId;
                accPolicyStamp.PolicyNumber        = model.PolicyNumber;
                accPolicyStamp.AccountType         = (int)PolicyAccountType.StampDuty;
                accPolicyStamp.Amount      = model.StampDuty.Value;
                accPolicyStamp.AccountName = PolicyAccountType.StampDuty.ToString();
                accPolicyStamp.Status      = model.Status;
                InsuranceContext.AccountPolices.Insert(accPolicyStamp);


                AccountPolicy accPolicyZtsc = new AccountPolicy();
                accPolicyZtsc.CreatedAt           = DateTime.Now;
                accPolicyZtsc.RecieptAndPaymentId = model.RecieptAndPaymentId;
                accPolicyZtsc.PolicyId            = model.PolicyId;
                accPolicyZtsc.PolicyNumber        = model.PolicyNumber;
                accPolicyZtsc.AccountType         = (int)PolicyAccountType.ZtscLevy;
                accPolicyZtsc.Amount      = model.ZtscLevy.Value;
                accPolicyZtsc.AccountName = PolicyAccountType.ZtscLevy.ToString();
                accPolicyZtsc.Status      = model.Status;
                InsuranceContext.AccountPolices.Insert(accPolicyZtsc);

                if (model.RadioLicenseCost > 0)
                {
                    AccountPolicy accPolicyRadioLic = new AccountPolicy();
                    accPolicyRadioLic.CreatedAt           = DateTime.Now;
                    accPolicyRadioLic.RecieptAndPaymentId = model.RecieptAndPaymentId;
                    accPolicyRadioLic.PolicyId            = model.PolicyId;
                    accPolicyRadioLic.PolicyNumber        = model.PolicyNumber;
                    accPolicyRadioLic.AccountType         = (int)PolicyAccountType.RadioLicense;
                    accPolicyRadioLic.Amount      = model.RadioLicenseCost.Value;
                    accPolicyRadioLic.AccountName = PolicyAccountType.RadioLicense.ToString();
                    accPolicyRadioLic.Status      = model.Status;
                    InsuranceContext.AccountPolices.Insert(accPolicyRadioLic);
                }

                if (model.ZinaraLicenseCost > 0)
                {
                    AccountPolicy accPolicyZinaraLic = new AccountPolicy();
                    accPolicyZinaraLic.CreatedAt           = DateTime.Now;
                    accPolicyZinaraLic.RecieptAndPaymentId = model.RecieptAndPaymentId;
                    accPolicyZinaraLic.PolicyId            = model.PolicyId;
                    accPolicyZinaraLic.PolicyNumber        = model.PolicyNumber;
                    accPolicyZinaraLic.AccountType         = (int)PolicyAccountType.ZinaraLicense;
                    accPolicyZinaraLic.Amount      = model.ZinaraLicenseCost.Value;
                    accPolicyZinaraLic.AccountName = PolicyAccountType.ZinaraLicense.ToString();
                    accPolicyZinaraLic.Status      = model.Status;
                    InsuranceContext.AccountPolices.Insert(accPolicyZinaraLic);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #9
0
        public async Task ChangePassword(MyUserAdapterModel myUserAdapterModel, string newPassword,
                                         string ip)
        {
            CleanTrackingHelper.Clean <AccountPolicy>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
            AccountPolicy AccountPolicy = await context.AccountPolicy
                                          .OrderBy(x => x.Id)
                                          .FirstOrDefaultAsync();

            string encodePassword =
                PasswordHelper.GetPasswordSHA(myUserAdapterModel.Salt, newPassword);

            myUserAdapterModel.Password = encodePassword;
            var myUser = Mapper.Map <MyUser>(myUserAdapterModel);

            CleanTrackingHelper.Clean <MyUser>(context);

            #region 更新下次要變更密碼的時間
            if (AccountPolicy.EnableCheckPasswordAge)
            {
                myUser.ForceChangePasswordDatetime = DateTime.Now
                                                     .AddDays(AccountPolicy.PasswordAge);
            }
            myUser.ForceChangePassword = false;
            #endregion

            context.Entry(myUser).State = EntityState.Modified;
            await context.SaveChangesAsync();

            if (AccountPolicy.EnablePasswordHistory == true)
            {
                MyUserPasswordHistory myUserPasswordHistory = new MyUserPasswordHistory()
                {
                    MyUserId = myUser.Id,
                    IP       = ip,
                    Password = myUser.Password,
                    ChangePasswordDatetime = DateTime.Now,
                };

                await context.AddAsync(myUserPasswordHistory);

                await context.SaveChangesAsync();

                while (true)
                {
                    #region 只會記錄下系統指定的變更密碼數量 AccountPolicy.PasswordHistory
                    var myUserPasswordHistories = await context.MyUserPasswordHistory
                                                  .Where(x => x.MyUserId == myUser.Id)
                                                  .OrderBy(x => x.ChangePasswordDatetime)
                                                  .ToListAsync();

                    if (myUserPasswordHistories.Count > AccountPolicy.PasswordHistory)
                    {
                        var first = myUserPasswordHistories.First();
                        context.Remove(first);
                        await context.SaveChangesAsync();

                        continue;
                    }
                    else
                    {
                        break;
                    }
                    #endregion
                }
            }
            CleanTrackingHelper.Clean <AccountPolicy>(context);
            CleanTrackingHelper.Clean <MyUser>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
        }