Net Mvc 统一登陆验证

通过两个自定义特性 ,实现统一登陆验证。

1. 用于登陆NeedLoginAttribute;

 public class NeedLoginAttribute : ActionFilterAttribute
    {
        private static string _loginUrl = ConfigurationManager.AppSettings["LoginUrl"]?? "/Login";
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
            //判断Action方法的Control是否跳过登录验证
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }
            //判断Action方法是否跳过登录验证
            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }
            if (true)
            {
                
                filterContext.HttpContext.Response.Redirect(_loginUrl);
                
            }
        }
    }

2. 免于登录UnNeedLoginAttribute;

public class UnNeedLoginAttribute : System.Attribute
{
}

3. 使用方式。

1)创建baseController,所有的控制器继承这个基类,这样就可以在访问action的时候先进行登陆验证了;

[NeedLogin]
    public class BaseController : Controller
    {}

2)但是,但是存在登陆页面的控制器继承了基类,但是又不希望登陆,因为本身不是登陆状态,验证登陆只会死循环。那么该怎么做呢?如下:

 [UnNeedCheckLogin]
    public class LoginController : BaseController
    {}

到此,MVC的统一登陆验证就实现完毕!是不是很简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值