MVC Custom Attribute Validation Login

Source: Internet
Author: User
Tags httpcontext

1. Create a new custom attribute class

 Public classBasefilterattribute:filterattribute, Iauthorizationfilter {/// <summary>        ///Custom Extended properties, verifying user logon/// </summary>        /// <param name= "Filtercontext" >The AuthorizationContext class encapsulates information about the controller, the HTTP context, the request context, the routing data, the action descriptor, and the result of the operation. </param>         Public voidonauthorization (AuthorizationContext filtercontext) {if(Filtercontext.httpcontext = =NULL)            {                Throw NewException ("The HTTP context does not exist! "); }            if(FilterContext.HttpContext.Session = =NULL)            {                Throw NewException ("Server session is not available! "); }            if(FilterContext.ActionDescriptor.IsDefined (typeof(Allowanonymousattribute),true))return; if(Filtercontext.httpcontext.session[confighelper.sessioncookiekey]! =NULL)return; //if (filtercontext.httpcontext.request.cookies[confighelper.sessioncookiekey]==null)//Filtercontext.result = new Redirectresult ("~/views/account/login.cshtml");            if(Cookieshelper.getcookie (confighelper.sessioncookiekey) = =NULL) Filtercontext.result=NewRedirectresult (Confighelper.sessioncookiereturnurl); }    }

2. Add the custom attribute to the associated controller class

[Basefilter]      Public class Prelistcontroller:basecontroller    {    }

3. Set up methods that are not validated to allow anonymous access

        [allowanonymous]        public  actionresult Login ()        {            return  View ();        }

MVC Custom Attribute Validation Login

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.