MVC Filter Build member is logged in

Source: Internet
Author: User

Use of the environment: in the background management or the front desk has a member center in the case of use

Usage: This is a User Center controller I add a feature to the User Center home page

[Accountfilter] This is the name of a filter

 Public class Accountcontroller:controller    {        [accountfilter]        public  actionresult Index ()        {            return  View ();        }    }

Filter specific code is as follows

namespacefytmsys.helper{/// <summary>    ///Foreground User Login filter/// </summary>[AttributeUsage (attributetargets.all, AllowMultiple =true)]     Public classAccountfilterattribute:actionfilterattribute { Public stringMessage {Get;Set; }        Filtercontextinfo _fcinfo; //called by the ASP. OnActionExecuted after executing the action method. //called by the ASP. OnActionExecuting before executing the action method. //called by the ASP. Onresultexecuted after executing the result of the operation. //Onresultexecuting is called by the ASP. NET before executing the result of the operation.         /// <summary>        ///called by the ASP framework before the action method is executed. /// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {_fcinfo=NewFiltercontextinfo (Filtercontext); if(Utilshelper.getcookie ("Ddhuser") =="") {Filtercontext.result=NewRedirecttorouteresult (NewSystem.Web.Routing.RouteValueDictionary (New{Controller ="Users", action ="Login" })); }        }        /// <summary>        ///called by the ASP framework after the action method is executed. /// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonactionexecuted (ActionExecutedContext filtercontext) {Base.        OnActionExecuted (Filtercontext); }        /// <summary>        ///called by the ASP. Onresultexecuted after executing the result of the operation. /// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonresultexecuted (ResultExecutedContext filtercontext) {Base.        Onresultexecuted (Filtercontext); }        /// <summary>        ///Onresultexecuting is called by the ASP. NET before executing the result of the operation. /// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonresultexecuting (ResultExecutingContext filtercontext) {Base.        Onresultexecuting (Filtercontext); }         Public classFiltercontextinfo { PublicFiltercontextinfo (ActionExecutingContext filtercontext) {#regionGet the characters in a link//Get domain name                if(FilterContext.HttpContext.Request.Url! =NULL) DomainName=filterContext.HttpContext.Request.Url.Authority; //Get module name//module = filtercontext.httpcontext.request.url.segments[1]. Replace ('/', ').                Trim (); //Get controllername nameControllername = filtercontext.routedata.values["Controller"].                ToString (); //get the action nameActionName = filtercontext.routedata.values["Action"].                ToString (); #endregion            }            /// <summary>            ///Get domain name/// </summary>             Public stringDomainName {Get;Set; } /// <summary>            ///Get module name/// </summary>             Public stringModule {Get;Set; } /// <summary>            ///Get controllername name/// </summary>             Public stringcontrollername {Get;Set; } /// <summary>            ///get the action name/// </summary>             Public stringActionName {Get;Set; } }    }}

I used the OnActionExecuting override method, which means that the filter is called before executing the method

Determine if the user is logged in, if not logged in, jump directly to the login page

is not very simple, do not know how security, I am also the first time to use

Welcome to the Great God.

ASP. NET MVC: 86594082 Welcome to your arrival.

MVC Filter Build member is logged in

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.