Standard for the simplest authentication of ASP. NET MVC [authorize]

Source: Internet
Author: User
Tags httpcontext

    [Authorize]        public  contentresult Index2 ()        {            return Content ( " the validation passed " );        

It is often possible to see an action under a controler to verify the state of the login permission through this tag, otherwise the action will not execute. But what conditions can be met to pass authorize this filter to check it? Answer: ↓

  Publicwebapiapplication () {postauthenticaterequest+=webapiapplication_postauthenticaterequest; }        /// <summary>        ///gets the user's identity information/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidWebapiapplication_postauthenticaterequest (Objectsender, EventArgs e) {            //Authentication Logic//Key CodeHttpContext.Current.User =NewMyUser () {code ="Admin", name ="Admin" }; }

Add the following code to the Global.asax. This means that in the application pipeline event, "Get the user identity information" in the event, obtain the user identity information and verify, give HttpContext.Current.User assignment. It is important to note that the myuser needs to implement the IPrincipal interface.

Sentiment:

Every day is talking about a variety of advanced technology, all kinds of loading force, the simplest basic applications are not know, code with a half-day to find, the problem is asked a circle do not know, hey ...

Standard for the simplest authentication of ASP. NET MVC [authorize]

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.