Ejemplo n.º 1
0
        public async Task InitDBAsync()
        {
            Random random = new Random();

            #region 適用於 Code First ,刪除資料庫與移除資料庫
            string Msg = "";
            Msg = $"適用於 Code First ,刪除資料庫與移除資料庫";
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureDeletedAsync();

            Msg = $"刪除資料庫";
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureCreatedAsync();

            Msg = $"建立資料庫";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion
        }
Ejemplo n.º 2
0
        public async Task OnSaveAsync()
        {
            Relogin = false;

            MyUserAdapterModel myUserAdapterModel = new MyUserAdapterModel();

            #region 進行 Form Validation 檢查驗證作業
            if (LocalEditContext.Validate() == false)
            {
                return;
            }
            #endregion

            #region 其他資料完整性驗證
            if (ChangePasswordModel.NewPasswordAgain != ChangePasswordModel.NewPassword)
            {
                MessageBox.Show("400px", "200px",
                                ErrorMessageMappingHelper.Instance.GetErrorMessage(ErrorMessageEnum.警告),
                                ErrorMessageMappingHelper.Instance.GetErrorMessage(ErrorMessageEnum.新密碼2次輸入須相同),
                                CloseMessageBox);
                return;
            }
            else
            {
                myUserAdapterModel = await ChangePasswordService.GetCurrentUser();

                if (myUserAdapterModel == null)
                {
                    MessageBox.Show("400px", "200px",
                                    ErrorMessageMappingHelper.Instance.GetErrorMessage(ErrorMessageEnum.警告),
                                    ErrorMessageMappingHelper.Instance.GetErrorMessage(ErrorMessageEnum.使用者不存在),
                                    CloseMessageBox);
                    return;
                }
            }
            #endregion

            #region 進行密碼變更
            await ChangePasswordService.ChangePassword(myUserAdapterModel, ChangePasswordModel.NewPassword,
                                                       HttpContextAccessor.GetConnectionIP());

            Relogin = true;
            MessageBox.Show("400px", "200px",
                            ErrorMessageMappingHelper.Instance.GetErrorMessage(ErrorMessageEnum.警告),
                            ErrorMessageMappingHelper.Instance.GetErrorMessage(ErrorMessageEnum.密碼已經變更成功),
                            CloseMessageBox);

            #endregion
        }
Ejemplo n.º 3
0
        public async Task InitDataAsync(string InitializationMode, Action <string> OnUpdateMessage)
        {
            Random   random     = new Random();
            DateTime currentNow = DateTime.Now;

            context.Database.SetCommandTimeout(TimeSpan.FromMinutes(3));

            #region 適用於 Code First ,刪除資料庫與移除資料庫
            string Msg = "";
            Msg = $"適用於 Code First ,刪除資料庫與移除資料庫";
            OnUpdateMessage(Msg);
            Logger.LogInformation($"{Msg}");
            try
            {
                currentNow = DateTime.Now;
                Msg        = $"刪除資料庫";
                OnUpdateMessage($"{Msg}");
                await context.Database.EnsureDeletedAsync();

                OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
                Logger.LogInformation($"{Msg} ({DateTime.Now - currentNow})");
            }
            catch (Exception ex)
            {
                OnUpdateMessage($"{Msg} 發生例外異常 ({DateTime.Now - currentNow})");
                OnUpdateMessage(ex.Message);
                Logger.LogError(Msg, ex);
                return;
            }
            try
            {
                currentNow = DateTime.Now;
                Msg        = $"建立資料庫";
                OnUpdateMessage($"{Msg}");
                await context.Database.EnsureCreatedAsync();

                OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
                Logger.LogInformation($"{Msg} ({DateTime.Now - currentNow})");
            }
            catch (Exception ex)
            {
                OnUpdateMessage($"{Msg} 發生例外異常 ({DateTime.Now - currentNow})");
                OnUpdateMessage(ex.Message);
                Logger.LogError(Msg, ex);
                return;
            }
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立開發環境要用到的測試紀錄
            #region 建立功能表角色與項目清單
            try
            {
                currentNow = DateTime.Now;
                Msg        = $"建立功能表角色與項目清單";
                OnUpdateMessage(Msg);
                await 建立功能表角色與項目清單Async();

                OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            }
            catch (Exception ex)
            {
                OnUpdateMessage($"{Msg} 發生例外異常 ({DateTime.Now - currentNow})");
                OnUpdateMessage(ex.Message);
                Logger.LogError(Msg, ex);
                return;
            }
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立使用者紀錄
            await 建立使用者紀錄Async(InitializationMode);

            Msg = $"建立使用者紀錄";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            if (InitializationMode == "開發模式")
            {
            }
            #endregion

            Msg = $"資料庫初始化作業完成";
            OnUpdateMessage(Msg);
        }
Ejemplo n.º 4
0
        public async Task InitDataAsync()
        {
            Random random = new Random();

            #region 適用於 Code First ,刪除資料庫與移除資料庫
            string Msg = "";
            Msg = $"適用於 Code First ,刪除資料庫與移除資料庫";
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureDeletedAsync();

            Msg = $"刪除資料庫";
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureCreatedAsync();

            Msg = $"建立資料庫";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立開發環境要用到的測試紀錄
            #region 建立系統定義參數
            await 建立系統定義參數Async();

            Msg = $"建立系統定義參數";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立功能表角色與項目清單
            await 建立功能表角色與項目清單Async();

            Msg = $"建立功能表角色與項目清單";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立使用者紀錄
            await 建立使用者紀錄Async();

            Msg = $"建立使用者紀錄";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立片語分類與文字Async
            await 建立片語分類與文字Async();

            Msg = $"建立片語分類與文字";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立簽核政策
            await 建立簽核政策Async();

            Msg = $"建立簽核政策";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 建立派工單分類清單
            await 建立派工單分類清單Async();

            Msg = $"建立派工單分類清單";
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #endregion
        }
        public async Task InitDBAsync(string path, Action <string> OnUpdateMessage)
        {
            string content = await File.ReadAllTextAsync(path);

            InitDatas = JsonConvert.DeserializeObject <InitDatas>(content);
            Random random = new Random();

            context.Database.SetCommandTimeout(TimeSpan.FromMinutes(3));

            #region 適用於 Code First ,刪除資料庫與移除資料庫
            string Msg = "";
            Msg = $"適用於 Code First ,刪除資料庫與移除資料庫";
            OnUpdateMessage(Msg);
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureDeletedAsync();

            Msg = $"刪除資料庫";
            OnUpdateMessage(Msg);
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureCreatedAsync();

            Msg = $"建立資料庫";
            OnUpdateMessage(Msg);
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 還原預設紀錄
            DateTime currentNow;
            #region AccountPolicy
            currentNow = DateTime.Now;
            Msg        = $"建立 AccountPolicy";
            OnUpdateMessage(Msg);
            await context.AccountPolicy.AddRangeAsync(InitDatas.AccountPolicy);

            await context.SaveChangesWithoutIdentityInsertAsync <AccountPolicy>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MenuRole
            currentNow = DateTime.Now;
            Msg        = $"建立 MenuRole";
            OnUpdateMessage(Msg);
            await context.MenuRole.AddRangeAsync(InitDatas.MenuRole);

            await context.SaveChangesWithoutIdentityInsertAsync <MenuRole>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MenuData
            currentNow = DateTime.Now;
            Msg        = $"建立 MenuData";
            OnUpdateMessage(Msg);
            await context.MenuData.AddRangeAsync(InitDatas.MenuData);

            await context.SaveChangesWithoutIdentityInsertAsync <MenuData>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MyUser
            currentNow = DateTime.Now;
            Msg        = $"建立 MyUser";
            OnUpdateMessage(Msg);
            await context.MyUser.AddRangeAsync(InitDatas.MyUser);

            await context.SaveChangesWithoutIdentityInsertAsync <MyUser>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MyUserPasswordHistory
            currentNow = DateTime.Now;
            Msg        = $"建立 MyUserPasswordHistory";
            OnUpdateMessage(Msg);
            await context.MyUserPasswordHistory.AddRangeAsync(InitDatas.MyUserPasswordHistory);

            await context.SaveChangesWithoutIdentityInsertAsync <MyUserPasswordHistory>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion
            #endregion
        }
        public async Task <IActionResult> OnPostAsync()
        {
            await GetPasswordHint();

            PasswordStrength passwordStrength = (PasswordStrength)AccountPolicyAdapterModel.PasswordComplexity;

            ClaimsPrincipal claimsPrincipal = HttpContextAccessor.HttpContext.User;

            if (claimsPrincipal.Identity.IsAuthenticated == false)
            {
                Msg = "無法變更密碼,請先進行帳號密碼身分驗證程序";
                return(Page());
            }
            else if (NewPassword != AgainPassword)
            {
                Msg = "請確認兩次輸入的密碼都是相同的";
                return(Page());
            }
            else
            {
                var inputPasswordStrength = PasswordCheck.GetPasswordStrength(NewPassword);
                if (passwordStrength > inputPasswordStrength)
                {
                    Msg = "密碼強度不足,請輸入符合密碼政策的密碼";
                    return(Page());
                }
                var userId = Convert.ToInt32(claimsPrincipal.FindFirst(ClaimTypes.Sid)?.Value);
                var myUser = await myUserService.GetAsync(userId);

                if (myUser.Status == false)
                {
                    #region 使用者已經被停用,無法變更密碼
                    Msg = $"使用者 {myUser.Account} 已經被停用,無法變更密碼";
                    await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                    {
                        Message    = Msg,
                        Category   = LogCategories.User,
                        Content    = "",
                        LogLevel   = LogLevels.Information,
                        Updatetime = DateTime.Now,
                        IP         = HttpContextAccessor.GetConnectionIP(),
                    });

                    logger.LogInformation($"{Msg}");
                    return(Page());

                    #endregion
                }
                Msg = await changePasswordService.CheckWetherCanChangePassword(myUser, NewPassword);

                if (string.IsNullOrEmpty(Msg) == false)
                {
                    return(Page());
                }
                await changePasswordService.ChangePassword(myUser, NewPassword,
                                                           HttpContextAccessor.GetConnectionIP());

                Msg = $"使用者 {myUser.Account} / {myUser.Name} " +
                      $"已經變更密碼 {DateTime.Now}";
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.User,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });

                logger.LogInformation($"{Msg}");
            }

            string returnUrl = Url.Content("~/");
            return(LocalRedirect(returnUrl));
        }