using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
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
|
{
|
/// <summary>
|
/// 渠道过滤器
|
/// </summary>
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
|
public class ChannelActionFilterAttribute : ActionFilterAttribute
|
{
|
ToMessage mes = new ToMessage();
|
RedisHelper redis = new RedisHelper();
|
/// <summary>
|
/// 请求接口之前渠道过滤
|
/// </summary>
|
/// <param name="actionContext"></param>
|
public override void OnActionExecuting(HttpActionContext actionContext)
|
{
|
try
|
{
|
var User = HttpContext.Current.Session["User"].ToString();
|
User loginUser = JsonConvert.DeserializeObject<User>(User);
|
base.OnActionExecuting(actionContext);
|
//如果存在
|
if (redis.Get<User>("LoginUserIDPC" + loginUser.userid, 0).userid.ToString() != "")
|
{
|
//获取redis中当前用户信息
|
User r_loginUser = redis.Get<User>("LoginUserIDPC" + loginUser.userid, 0);
|
//如果session中的用户guid和redis中的用户guid匹配
|
if (r_loginUser.guid.Equals(loginUser.guid))
|
{
|
//重置过期时间
|
redis.Set<User>("LoginUserIDPC" + loginUser.userid, loginUser, redis.secondsTimeOut, 0);
|
}
|
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") };
|
}
|
|
}
|
}
|
}
|