过滤器实现 (cookie认证)

cookie用来做身份认证,非常好用,只需要设置Authentication和Authorization就行了。

但是 ,如果cookie不能用了,怎么办?  不要紧,我们也可以用过滤器进行身份认证。

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; //过滤器命名空间

namespace CMRC17.Models
{

//拦截
public class MyAuthorizeAttribute : ActionFilterAttribute //继承该类,重写过滤器方法
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//IsNullOrEmpty为空,如果cookie里面为空没有值,那么拦截,去登录页面
if (string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) //User.Identity.Name获取当前cookie里的值,
{
HttpContext.Current.Response.Redirect("/Home/Login");
}
base.OnActionExecuting(filterContext);
}
}
}

 

posted on 2018-10-29 10:38  阿里正正  阅读(808)  评论(0编辑  收藏  举报

导航