예제 #1
0
        public async Task <ActionResult> RefreshToken(string refreshToken)
        {
            if (!JWTService.Instance.IsTokenValid(refreshToken))
            {
                return(Json(new JsonMessage(1000, "invalid token")));
            }
            var tmp = JWTService.Instance.GetTokenClaims(refreshToken).FirstOrDefault(m => m.Type == "Type");

            if (tmp.Value.ToString() != "RefreshToken")
            {
                return(Json(new JsonMessage(1000, "invalid token")));
            }
            var phone = JWTService.Instance.GetTokenClaims(refreshToken).FirstOrDefault(m => m.Type == "Phone")?.Value.ToString();
            var id    = JWTService.Instance.GetTokenClaims(refreshToken).FirstOrDefault(m => m.Type == "ID")?.Value.ToString();
            var key   = Guid.Parse(JWTService.Instance.GetTokenClaims(refreshToken).FirstOrDefault(m => m.Type == "key")?.Value ?? throw new InvalidOperationException());

            var oldToke = _agencyDa.GetTokenByGuidId(key);

            if (oldToke == null)
            {
                return(Json(new JsonMessage(1000, "invalid token")));
            }
            _agencyDa.DeleteTokenRefresh(oldToke);
            key = Guid.NewGuid();

            IAuthContainerModel model = new JWTContainerModel()
            {
                Claims = new Claim[]
                {
                    new Claim("Phone", phone),
                    new Claim("Type", "Token"),
                    new Claim("ID", id),
                },
                ExpireMinutes = 10,
            };

            IAuthContainerModel modelRefreshToken = new JWTContainerModel()
            {
                Claims = new Claim[]
                {
                    new Claim("Phone", phone),
                    new Claim("key", key.ToString()),
                    new Claim("Type", "RefreshToken"),
                    new Claim("ID", id),
                },
                ExpireMinutes = 60 * 24 * 30,
            };

            var tokenResponse        = JWTService.Instance.GenerateToken(model);
            var refreshTokenResponse = JWTService.Instance.GenerateToken(modelRefreshToken);

            _agencyDa.InsertToken(new TokenRefresh()
            {
                GuidId = key
            });
            _agencyDa.Save();

            return(Json(new BaseResponse <CustomerAppIG4Item>()
            {
                Code = 200, Erros = false, Message = "", Data = new CustomerAppIG4Item()
                {
                    Token = tokenResponse, RefreshToken = refreshTokenResponse
                }
            }, JsonRequestBehavior.AllowGet));
        }