public static UserLog ToModel(this UserLogDTO ObjectToConvert)
 {
     return(new UserLog()
     {
         ID = ObjectToConvert.ID,
         IPAddress = ObjectToConvert.IPAddress,
         DateTimeLogged = ObjectToConvert.DateTimeLogged,
         User = ObjectToConvert.User.ToModel()
     });
 }
示例#2
0
        public async Task <IActionResult> Login(UserLogDTO user)
        {
            var result = new ServiceResult();

            var loginModel = await _userAppService.CheckUser(user.UserName, user.Password);

            if (loginModel == null)
            {
                result.IsFailed("账号或密码错误");
                return(Ok(result));
            }

            IEnumerable <Claim> claims = new Claim[]
            {
                new Claim(JwtClaimTypes.Id, loginModel.Id.ToString()),
                new Claim(JwtClaimTypes.Name, loginModel.UserName),
                new Claim(JwtClaimTypes.Role, "admin")
            };
            //生成允许访问的JWT
            var token = _CommonEven.GenerateAccessToken(claims);
            //生成允许刷新JWT的Token
            var refreshToken = _CommonEven.GenerateRefreshToken();

            loginModel.CreateRefreshToken(refreshToken, loginModel.UserName);
            result.IsFailed("发生未知错误");
            if (!await _userAppService.Save(loginModel))
            {
                throw new System.Exception("Throw Exception");
            }

            //更新登入时间
            loginModel.LastLoginTime = DateTime.Now;
            return(Ok(new
            {
                RefreshToken = refreshToken,
                access_token = token,
                token_type = "Bearer",
                resetTime = 3000,
                message = "登入成功",
                Error = false
            }));

            #region 废弃的

            #endregion
        }