MVC Filter登录验证

Login Controller

        public ActionResult Index()
        {
            //return Content("hello index!");
            return View();

        }
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string username, string password)
        {
            if(username=="111"&&password=="111")
            {
                Session["UUU"] = "111";
                return RedirectToAction("Index");
            }
            else
            {
                return Content("登录失败!");
            }

        }

Index View

<h2>Index</h2>
<text>主页运行成功</text>

Login View

<form action="/Login/Login" method="post">
    用户名<input type="text" name="username" value="" />
    密码<input type="text" name="password" value="" />
    <input type="submit" name="name" value="提交" />

</form>

CheckLoginFilter

using System.Web.Mvc;//别引用错了

 public class CheckLoginFilter:IAuthorizationFilter
    {

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            string actionName = filterContext.ActionDescriptor.ActionName;
            if (controllerName == "Login" && actionName == "Login")
            {

            }
            else
            {
                if (filterContext.HttpContext.Session["UUU"] == null)
                {
                    //ContentResult contexts = new ContentResult();
                    //contexts.Content = ("没有登录");
                    //filterContext.Result=contexts;
                    filterContext.HttpContext.Response.Redirect("/Login/Login");

                }
            }
        }
    }

Global

 RouteConfig.RegisterRoutes(RouteTable.Routes);
 GlobalFilters.Filters.Add(new CheckLoginFilter() );//添加filter

  

原文地址:https://www.cnblogs.com/lierjie/p/11920529.html

时间: 2024-08-01 04:05:43

MVC Filter登录验证的相关文章

Java Web Filter登录验证

初做网站需要登录验证,转自 :http://blog.csdn.net/daguanjia11/article/details/48995789 Filter: Filter是服务器端的组件,用来过滤web请求.当发生一个web请求时,web容器会先检查请求的URL是否设置了Filter,如果设置了,则执行该Filter的doFilter方法.所有Filter都实现了javax.servlet.Filter接口,doFilter是定义在该接口中的最重要的方法. 最常见的使用过滤器的例子有:登录访

Filter登录验证过滤器(全局)

通过Filter来定义一个登录验证过滤器,这是就不需要在每一个JSP页面添加判断用户合法性的代码了. 以下示例中包含了5个文件,一个是登录表单LoginForm.jsp,一个是登录判断页LoginConf.jsp, 一个是登录验证过滤器LoginFilter.java,一个是登录成功页面LoginSucess.jsp,一个是登录失败 页面Loginfailure.jsp. LoginForm.jsp 1 <%@ page language="java" import="

MVC Filter自定义验证(拦截)

1 namespace QS.Web.Extensions 2 { 3 /// <summary> 4 /// 验证session.权限 状态 5 /// </summary> 6 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] 7 public class RequestFilterAttribute : ActionFilterAttribute

asp.net MVC 通用登录验证模块

用法: 还是希望读者把源码看懂,即可运用自如.重点是,为什么有个UserType!!! 登录用户信息: namespace MVCCommonAuth { [Serializable] public class LoginUser { private const string DESKEY = "12345678"; public int ID { get; set; } public string UserName { get; set; } public string Roles

[MVC学习笔记]5.使用Controller来代替Filter完成登录验证(Session校验)

      之前的学习中,在对Session校验完成登录验证时,通常使用Filter来处理,方法类似与前文的错误日志过滤,即新建Filter类继承ActionFilterAttribute类后重写OnActionExecuting方法,在RegisterGlobalFilters方法中注册新建的Filter类,之后直接在需要验证的Action前加上Filter标记即可. 1. 新建登陆校验类CheckLoginAttribute using System.Web.Mvc; namespace P

ASP.NET MVC使用AuthenticationAttribute验证登录

首先,添加一个类AuthenticationAttribute,该类继承AuthorizeAttribute,如下: using System.Web; using System.Web.Mvc; namespace Zhong.Web { public class AuthenticationAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterConte

MVC模式编程示例-登录验证(静态)

好,上篇博客分享了本人总结的JSP-Servlet-JavaBean三层架构编程模式的实现思想和基本流程,接下来给大家分享一个MVC编程模式的实现示例-登录验证的过程,这里我仍然用的是静态的验证用户名和密码,连接数据库的过程我其他博客有,这里只是把一个实现过程告诉大家,不多说,见代码: 首先建一个JSP登录页面 其次,我们建一个Servlet类获取客户端提交的信息 然后,我们需要在web.xml配置Servlet选项,好像现在的Eclipse都是自动生成了Servlet的配置代码,不过大家还是看

Filter实现登录验证拦截功能

既然是 Filter 实现的登录验证,首先自然是创建 Filter 实现类了,但是在创建 Filter 之前,我们先来看一个问题:怎么判断用户是否登录了? 看我们的登录代码: @RequestMapping("/login") public String login(LoginModel login, HttpServletRequest req, HttpServletResponse res, Model model) { HttpSession session = req.get

MVC过滤器进行统一登录验证

统一登录验证: 1.定义实体类Person:利用特性标签验证输入合法性设计登录页面 1 2 3 4 5 6 7 8 9 public class Person {     [DisplayName("用户名"), Required(ErrorMessage = "账户非空!")]     public string LoginName { get; set; }     [DisplayName("密 码"), Required(ErrorMes