Esempio n. 1
0
        public JsonResult RefreshToken([FromForm]  string token)
        {
            ResponseResult result = new ResponseResult();

            if (string.IsNullOrEmpty(token))
            {
                result.Code = ResponseResultMessageDefine.ParaError;
                result.Errors.Add(ResponseResultMessageDefine.ParaErrorMessage);
            }
            else
            {
                TokenModelJWT tokenModel = JwtHelper.DerializeJWT(token);

                if (tokenModel.Uid > 0 && tokenModel.ExpDate > new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds())
                {
                    result.Data = JwtHelper.SerializeJWT(tokenModel);
                }
                else
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
            }

            return(Json(result));
        }
Esempio n. 2
0
        public virtual void OnAuthorization(AuthorizationFilterContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException(nameof(filterContext));
            }

            var result = filterContext.HttpContext.Request.Headers.TryGetValue("Authorization", out StringValues authStr);

            if (!result || string.IsNullOrEmpty(authStr.ToString()))
            {
                filterContext.Result = new UnauthorizedResult();
            }
            else
            {
                var jwtuserModel = JwtHelper.DerializeJWT(authStr.ToString());

                if (jwtuserModel != null && jwtuserModel.ExpDate > new DateTimeOffset(DateTime.Now.AddHours(1)).ToUnixTimeSeconds())
                {
                    var identity = new CustomIdentity(jwtuserModel.Uid.ToString());

                    var principal = new ClaimsPrincipal(identity);

                    filterContext.HttpContext.User = principal;
                }
            }
        }