using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using VueWebApi.Models;
namespace VueWebApi.Tools
{
///
/// 渠道过滤器
///
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class ChannelActionFilterAttribute : ActionFilterAttribute
{
ToMessage mes = new ToMessage();
RedisHelper redis = new RedisHelper();
///
/// 请求接口之前渠道过滤
///
///
public override void OnActionExecuting(HttpActionContext actionContext)
{
try
{
var rediskey = HttpContext.Current.Request.Cookies["rediskey"].Value.ToString();
var guid = HttpContext.Current.Request.Cookies["guid"].Value.ToString();
base.OnActionExecuting(actionContext);
//如果存在
if (redis.Get(rediskey, 0).userid.ToString() != "")
{
//获取redis中当前用户信息
User r_loginUser = redis.Get(rediskey, 0);
//如果session中的用户guid和redis中的用户guid匹配
if (r_loginUser.guid.Equals(guid))
{
//重置过期时间
redis.KeyExpire(rediskey, redis.secondsTimeOut);
}
else
{
mes.code = "303";
mes.Message = "登录已超时,请重新登录!";
actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
}
}
else //如果redis登录用户中不存在当前用户
{
mes.code = "303";
mes.Message = "登录已超时,请重新登录!";
actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
}
}
catch (Exception)
{
mes.code = "303";
mes.Message = "登录已超时,请重新登录!";
actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
}
}
}
}