예제 #1
0
        public async Task <IActionResult> Get()
        {
            APIResult apiResult;

            #region 建立查詢物件
            DataRequest dataRequest = new DataRequest()
            {
                Skip   = 0,
                Take   = 0,
                Search = "",
                Sorted = null,
            };
            #endregion

            var records = await myUserService.GetAsync(dataRequest);

            var result = mapper.Map <List <MyUserDto> >(records.Result);
            apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK,
                                               ErrorMessageEnum.None, payload: result);
            return(Ok(apiResult));
        }
예제 #2
0
        public async Task <IActionResult> RefreshToken()
        {
            APIResult apiResult;
            await Task.Yield();

            LoginRequestDto loginRequestDTO = new LoginRequestDto()
            {
                Account = User.FindFirst(ClaimTypes.Sid)?.Value,
            };

            MyUserAdapterModel user = await myUserService.GetAsync(Convert.ToInt32(loginRequestDTO.Account));

            if (user.Id == 0)
            {
                apiResult = APIResultFactory.Build(false, StatusCodes.Status401Unauthorized,
                                                   ErrorMessageEnum.沒有發現指定的該使用者資料);
                return(BadRequest(apiResult));
            }

            string token        = GenerateToken(user);
            string refreshToken = GenerateRefreshToken(user);

            LoginResponseDto LoginResponseDTO = new LoginResponseDto()
            {
                Account                = loginRequestDTO.Account,
                Id                     = 0,
                Name                   = loginRequestDTO.Account,
                Token                  = token,
                TokenExpireMinutes     = tokenConfiguration.JwtExpireMinutes,
                RefreshToken           = refreshToken,
                RefreshTokenExpireDays = tokenConfiguration.JwtRefreshExpireDays,
            };

            apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK,
                                               ErrorMessageEnum.None, payload: LoginResponseDTO);
            return(Ok(apiResult));
        }
        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));
        }