The attribute feature is used in MVC to perform simple login verification.

Source: Internet
Author: User

The previous period of time has been relatively busy. It was a very busy end. Idle nothing dry, churn the next MVC (EF), because it was used in three-layer framework to develop, MVC is not a lot of ... As we all know, in the three floor we are generally built a base class, and then in the base class to write the authentication login method, and then the need to verify the login page inherit the base class can ... But in MVC, all the view page operations are transferred to the controller. This time we are in accordance with the three-tier approach to build a base class to inherit the authentication login, there is no way to go through ... Today I'm going to show you one. Use the attribute feature to verify login. If you do not know this stuff, you can baidu a bit attribute.

First we build a BaseAdminPage.cs base class.

1  Public classBaseadminpage:authorizeattribute2     {3          PublicAdmin_sysuser Base_sysuser =NewAdmin_sysuser ();4 5          Public Override voidonauthorization (AuthorizationContext filtercontext)6         {7             #regionCheck if you are logged in8 9Admin_sysuser su =NewAdmin_sysuser ();Ten             if(Cookiemanage.checksysuserisloginvalid ( outsu)) One             { ASu. Lastlogindate =DateTime.Now; -Usermanage<admin_sysuser>. Updateadminsysuser (su); -  the                  This. Base_sysuser =su; -             } -             Else -             { +                  This. Loginout (); -             } +  A             #endregion at         } -         /// <summary> -         ///exit operation Support System -         /// </summary> -         protected voidloginout () -         { in Cookiemanage.clearsysuserlogincookie (); -HttpContext.Current.Response.Write ("<script type=\ "text/javascript\" >window.parent.location.href=\ "/adminmanage/login\";</script> "); to         } +}

Base class inherits Authorizeattribute this feature

And then we're in through rewriting

Attribute, onauthorization This method to implement authentication login
In this step we complete the base class login verification: So how do we call it?
Next we add a controller named Sysusercontroller.
If we only need a single operation for login verification, then we just have to look at the code below
public class Sysusercontroller:controller    {        ////        GET:/sysuser/        [controls.baseadminpage]//        Public ActionResult Index ()        {            return View ();        }    }

If the entire controller needs to verify the login, then we just need to change the code slightly, as follows

1 [Controls.baseadminpage]2      Public classSysusercontroller:controller3     {4         //5         //GET:/sysuser/6          Publicactionresult Index ()7         {8             returnView ();9         }Ten  One}

At this point in MVC we have completed the login verification ....

method is relatively simple to write. I hope you will forgive me ...

The attribute feature is used in MVC to perform simple login verification.

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.