过滤器实现 (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);
}
}
}

 

转载于:https://www.cnblogs.com/liuzheng0612/p/9869269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值