Ejemplo n.º 1
0
        /// <summary>
        /// 身份令牌验证
        /// </summary>
        /// <param name="actionContext"></param>
        protected virtual string TokenVerification(HttpActionContext actionContext)
        {
            // 获取token
            var token = GetToken(actionContext);

            if (string.IsNullOrEmpty(token))
            {
                actionContext.Response = actionContext.Request.CreateResponse <ResultData>(System.Net.HttpStatusCode.OK,
                                                                                           new ResultData(ResultType.Unauthorized, EnumExtension.GetEnumDescription(ResultType.Unauthorized)));
            }
            // 判断token是否有效
            else if (!UserTokenManager.IsExistToken(token))
            {
                actionContext.Response = actionContext.Request.CreateResponse <ResultData>(System.Net.HttpStatusCode.OK,
                                                                                           new ResultData(ResultType.Unauthorized, "已过期,请重新登录"));
            }

            return(token);
        }
Ejemplo n.º 2
0
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            // 验证token
            //var token = actionContext.Request.Headers.Authorization;
            var ts = actionContext.Request.Headers.Where(c => c.Key.ToLower() == "token").FirstOrDefault().Value;

            if (ts != null && ts.Count() > 0)
            {
                var token = ts.First <string>();
                // 验证token
                if (!UserTokenManager.IsExistToken(token))
                {
                    return(false);
                }
                return(true);
            }

            if (actionContext.Request.Method == HttpMethod.Options)
            {
                return(true);
            }
            return(false);
        }