首页 > 其他 > 详细

过滤器实现 (cookie认证)

时间:2018-10-29 11:04:26      阅读:196      评论:0      收藏:0      [点我收藏+]

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);
}
}
}

 

过滤器实现 (cookie认证)

原文:https://www.cnblogs.com/liuzheng0612/p/9869269.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!