public class MyAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { //return base.AuthorizeCore(httpContext); return DateTime.Now.Minute % 2 == 0 } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { filterContext.HttpContext.Response.Redirect("/Customer/Login"); //base.HandleUnauthorizedRequest(filterContext); } }
用asp.net mvc的方法好象不行啊。指点一下呗
看了一下还是有点懵
[Authorize] public IActionResult Forbidden() { //获取cookie信息 string strName = User.FindFirstValue(ClaimTypes.Name); string strGroup = User.FindFirstValue(ClaimTypes.GroupSid); string strEmail = User.FindFirstValue(ClaimTypes.Email); ViewData["strName"] = strName; ViewData["strGroup"] = strGroup; ViewData["strEmail"] = strEmail; return View(); }
其实我想重写Authorize的原因是,是想在每次进行授权认证的时候进行一些操作。就比如像asp.net mvc一样
public class MyAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { //return base.AuthorizeCore(httpContext); return DateTime.Now.Minute % 2 == 0 } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { filterContext.HttpContext.Response.Redirect("/Customer/Login"); //base.HandleUnauthorizedRequest(filterContext); } }
继承类,然后在自己的类里面写特殊操作。
然后授权的时候用
[MyAuthorize]
不知道core能不能做到这样。
@87Super: 参考之前的一个博问:Dotnet Core自定义全局过滤器[AllowAnonymous]标签无效