zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    Android 从上层到底层-----kernel层
    同时支持来自多个源头的域名的跨域调用
    Redis
    很好用的工具网站
    PHP array_combine()
    php 统计某个目录中所有文件的大小
    strchr()
    PHP $_SERVER
    Laravel5使用QQ邮箱发送邮件配置
    laravel 5.6
  • 原文地址:https://www.cnblogs.com/lierjie/p/11920529.html
Copyright © 2011-2022 走看看