Пример #1
0
        public static TokenConfig GetToken()
        {
            var token = new TokenConfig();

            token.Token = token.GetTokenItems();
            return(token);
        }
Пример #2
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var tokenHeader = HttpContext.Current.Request[X_TOKEN_NAME] ?? "";


            if (!actionContext.ModelState.IsValid)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
                return;
            }
            if (actionContext.ActionDescriptor.GetCustomAttributes <AllowAnonymousAttribute>().Any() ||
                actionContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes <AllowAnonymousAttribute>().Any())
            {
                return;
            }

            var request     = actionContext.Request;
            var tokenConfig = TokenConfig.GetToken();
            var accessToken = tokenConfig.GetTokenItems().AccessToken;

            if (string.IsNullOrEmpty(accessToken))
            {
                actionContext.Response = actionContext.Request.CreateResponse(
                    HttpStatusCode.Forbidden,
                    new { Success = false, Message = "Session expired." },
                    actionContext.ControllerContext.Configuration.Formatters.JsonFormatter
                    );
                return;
            }


            if ((request.Headers.Contains(X_TOKEN_NAME) &&
                 request.Headers.GetValues(X_TOKEN_NAME).First() == GeneralService.EncryptText(accessToken)) ||
                (tokenHeader != "" && tokenHeader == GeneralService.EncryptText(accessToken)))
            {
                tokenConfig.UpdateTokenExpireTime();
                return;
            }

            actionContext.Response = actionContext.Request.CreateResponse(
                HttpStatusCode.Unauthorized,
                new { Success = false, Message = "Unauthorized access." },
                actionContext.ControllerContext.Configuration.Formatters.JsonFormatter
                );
        }