MVC 5 AllowAnonymous无效

Se_先森_ 2017-01-04 11:21:44
使用MVC5, 自定义了一个FilterAttribute,类的结构如下
using AHF.Haohuo.Tools.EncryptionDecryption;
using AHF.Haohuo.Tools.Logger;
using AHF.Haohuo.Tools.Redis;
using AHF.Haohuo.Web.Parameter;
using AHF.Haohuo.Web.Tools;
using Newtonsoft.Json;
using System;
using System.Web;
using System.Web.Mvc;

namespace AHF.Haohuo.Web.Filters
{
/// <summary>
/// Token访问验证, 加上此特性, 代表该Action都需要验证token是否正确. 默认所有的Action都要进行token验证
/// 如某个Action不需要进行验证, 请在Action上加[AllowAnonymous]特性
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class ToKenCheckAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var cookie = filterContext.HttpContext.Request.Cookies[WebConfigs.UserTokenName];
if (cookie == null)
{
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.TokenCheckError}&path={WebConfigs.HomePath}", true);
return;
}
Token token = JsonConvert.DeserializeObject<Token>(CloudCharMD5.MD5Decrypt(cookie.Value));
if (token == null)
{
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.TokenCheckError}&path={WebConfigs.HomePath}", true);
return;
}
Token redisToken = null;
using (RedisOperator redis = new RedisOperator())
{
redisToken = redis.Get<Token>(token.ItemGuid, RedisDataBase.User);
}
if (redisToken == null)
{
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.TokenCheckError}&path={WebConfigs.HomePath}", true);
return;
}
try
{
if (token.ItemGuid != redisToken.ItemGuid)
{
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.TokenCheckError}&path={WebConfigs.HomePath}", true);
this.ClearUserToken(token.ItemGuid);
return;
}
if (token.UserAgent != redisToken.UserAgent)
{
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.BrowserChange}&path={WebConfigs.HomePath}", true);
this.ClearUserToken(token.ItemGuid);
return;
}
if (token.IP != redisToken.IP)
{
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.IpChange}&path={WebConfigs.HomePath}", true);
this.ClearUserToken(token.ItemGuid);
return;
}
if ((DateTime.Now - token.Date).Minutes >= WebConfigs.UserGuoqiTime)
{
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.TokenGuoqi}&path={WebConfigs.HomePath}", true);
this.ClearUserToken(token.ItemGuid);
return;
}
token.Date = DateTime.Now;
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(token.ItemGuid) { Value = CloudCharMD5.MD5Encrypt(JsonConvert.SerializeObject(token)) });
using (RedisOperator redis = new RedisOperator())
{
redis.SetExpire(token.ItemGuid, DateTime.Now.AddMinutes(WebConfigs.UserGuoqiTime), RedisDataBase.User);
}
}
catch (Exception ex)
{
LoggerManager.Error(ex);
filterContext.HttpContext.Response.Redirect($"/Base/Alert?msg={MsgConfig.TokenCheckError}&path={WebConfigs.HomePath}", true);
this.ClearUserToken(token.ItemGuid);
return;
}
}

/// <summary>
/// 验证失败, 清除redis
/// </summary>
/// <param name="key">Key</param>
private void ClearUserToken(string key)
{
using (var redis = new RedisOperator())
{
redis.Remove(key, RedisDataBase.User);
}
}
}
}

然后在父类上面加了[ToKenCheck], 然后在不需要验证的Action上加[AllowAnonymous,HttpGet], 但是无效.求解决方案
...全文
267 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2017-01-04
  • 打赏
  • 举报
回复
父类上面加了[ToKenCheck] 是指什么?

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧