MVC | Landing Filter

Source: Internet
Author: User
Tags httpcontext

Landing Filter

Using System;
Using System.Collections.Generic;
Using System.Linq;

Using System.Web;

    Namespace Mvcappfilter.filters {using SYSTEM.WEB.MVC; 
        To customize a myauattribute to allow this class to inherit the Authorizeattribute class, this class becomes a login validation filter public class Myauattribute:authorizeattribute { Overrides the parent class Onauthorization method.
        We are here to determine whether the user name session is empty, if it is empty to indicate that the user is not logged in, we can let it jump to the landing page. public override void Onauthorization (AuthorizationContext filtercontext) {/* if (FILTERC Ontext. Actiondescriptor.actionname! = "Login") {//If the login session is empty then if (filtercontext. httpcontext.session["Login"] = = null) {Filtercontext.result = new Redirectresult ("/ho
                Me/login "); }} *//If you have access to the Controller class on the [Skipchecklogin] attribute label//if (filtercontext.actionde Scriptor.


  Controllerdescriptor.isdefined (typeof (Loginattribute), false))//{//}          If you have access to the action on the [Skipchecklogin] attribute label if (filterContext.ActionDescriptor.IsDefined (typeof (Skipchec Kloginattribute) (false)) {//If the login session is empty, if (filterContext.HttpContext.Se ssion["Login"] = = NULL) {//First > Jump to Landing page (page URL will become Http://localhost:59075/Home/Logi

                    N) filterContext.HttpContext.Response.Redirect ("/home/login");
                    The second > or use this (the page URL will become http://localhost:59075/Home/Login) contentresult CR = new Contentresult (); Cr. Content = "<script>alert (' You have not logged in, please login, the page is about to jump to the landing page ');
                    window.location= '/home/login ' </script> ';

                    Filtercontext.result = CR; A third > or use this: replace the result of the view with the login view under the Home controller (the page URL is the same, but the view is replaced) Filtercontext.result = new Redirectresult (

                    "/home/login");
                   Fourth > or in this way (the page URL is the same, but the view is replaced) ViewResult VR = new ViewResult (); Vr.
                    ViewName = "/views/home/login.cshtml"; Vr.
                    ViewName = "/views/shared/tologin.cshtml";
                Filtercontext.result = VR; }}//base.
        Onauthorization (Filtercontext); }
    }
}

Landing Filter Perfect

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Threading.Tasks; Namespace FB. Cms.
    webhelper.filters {using SYSTEM.WEB.MVC;
    Using FB.CMS.Common; Using FB. Cms.
    Iservices; Using FB. Cms.
    Model.modelview;
    Using AUTOFAC;
    Using System.Web.WebPages; Using FB. Cms.
    Webhelper.attribute; Using FB. Cms.
    Model; public class Logincheckattribute:authorizeattribute {public override void Onauthorization (Authorizationcont Ext filtercontext) {string Controllernam = FilterContext.ActionDescriptor.ControllerDescr Iptor.
            Controllername; If there is a Skipcheckloginattribute attribute label on the controller class, return directly if (FilterContext.ActionDescriptor.ControllerDescriptor.IsDefi
            Ned (typeof (Skipcheckloginattribute), false)) {return; }//If there is a Skipcheckloginattribute attribute tag on the control action method, return directly if (Filtercontext.actiondEscriptor.
            IsDefined (typeof (Skipcheckloginattribute), false)) {return;
            }//sysuserinfo u = filtercontext.httpcontext.session[keys.uinfo] as SysUserInfo; If neither the controller nor the action method has a Skipcheckloginattribute attribute tag, check if the login Session exists if (filtercontext.httpcontext.session[keys.u Info]==null) {//If the user has not done the ability to remember the password.
                    Then skip to the landing page if (filtercontext.httpcontext.request.cookies[keys.isremember] = = null) {
                    FilterContext.HttpContext.Response.Redirect ("/admin/login/login");
                Return }//Get the user ID stored in the cookie (the reason why the aint () method is used to convert the string to int type,//is because if cookies[keys.i If the sremember does not exist, then a null value is taken, if an int is used. Parse (NULL) will error,//and the Asint () method, even if the conversion is unsuccessful, will return a 0, so there will be no error. String uid = Filtercontext.httpcon Text. Request.cookies[keys.isremember].
                Value; int UserID =Uid.

                Asint (); var container = cachemgr.getdata<icontainer> (KEYS.AUTOFAC); Get AUTOFAC's work container isysuserinfoservices Uinfo = container from the cache. Resolve<isysuserinfoservices> ();//Gets the Isysuserinfoservices class instance object from the container sysuserinfo userinfo = Uinfo. Querywhere (r = r.uid = = userid).
                    FirstOrDefault ();//Call the Querywhere method of the Isysuserinfoservices class if (userinfo = = null) { Even if the browser passes a coookie[keys.isremember], but we get the value according to the cookie key,//After the database query, found that no such user, so also need to jump to the landing page (Coookie[keys.isremember] The user's uid is stored in it) filterContext.HttpContext.Response.Redirect (
                    "/admin/login/login");
                Return

            } Filtercontext.httpcontext.session[keys.uinfo] = UserInfo;
            } else {return; }
        }

    }
}



Home Controller

Using Mvcappfilter.filters;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;

Namespace Mvcappfilter.controllers
{public
    class Homecontroller:controller
    {
        //Custom skip Check use of Login Properties tab
        [Skipchecklogin] public
        actionresult Index ()
        {
            return View ();
        }

        Public ActionResult Login ()
        {
            return View ();}}
}

Skipcheckloginattribute Attribute Class

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;

Namespace Mvcappfilter.filters
{
    [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)] public
    class Skipcheckloginattribute:attribute
    {

    }
}


Tologin view in this view can also do jump to the landing page, this view is more beautiful, the purpose is to put some ads, and prompt users to jump to the landing page

@{
    Layout = null;
}

<! DOCTYPE html>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.