Login permissions Verify session and cookie usage and BasePage class use

Source: Internet
Author: User
Tags empty

Recently in doing ASP.net project, access to the landing permission module, all summed up the landing of the knowledge and method skills.

As shown in the illustration: the effect of implementation, as shown in the diagram, because the verification code verification is relatively simple here is not introduced

First build the project with the code generator, take the three layer as an example, then I take the Bloguser table as an example, and then there will be a Bloguser entity class object.

The idea of landing: when we landed in the successful time to the entity into the session, no landing is also the idea, but when we tick-free landing, the first login, then the user's ID into the cookie, through the user ID to find the user's entity, Then assign the value to the session, then the session is not empty, you can achieve the free landing. So what is our basepage? First basepage inside is other pages of the parent class, only by landing to allow users to access other pages, then we use the Pagebase class, and then in BasePage to judge, if the session is empty, Then the user did not log in to jump to the landing page so that users can access other pages.

First: The main code of the front landing:

BLOGUSERBLL BLL = new BLOGUSERBLL (); 1. Go to the database to determine whether the username and password are correct list<bloguser> List = BLL.
                Getmodellist ("Loginname= '" + name + "' and loginpwd= '" + md5pwd + ""); 2. Determine if the user collection exists if (list). count>0) {//3. Save user information to session context.session["uinfo"] = List
                    [0]; 4. Determine if the user has chosen to remember to login if (!string).
                        Isnullorwhitespace (Remember)) {//5. Remember to save the user ID in a cookie HttpCookie cookie = new HttpCookie ("UID", list[0].
                        Id.tostring ()); Cookie.
                        Expires = DateTime.Now.AddDays (3);
                    CONTEXT.RESPONSE.COOKIES.ADD (cookie);
                Response.Write ("<script>alert success"); Window.location= ' bloglist.aspx ' </script> '); else {Response.Write ("<scriPt>alert (' Login failed, please login '); window.location= '/login.aspx ' </script> '); } response.end ();

Second: Pagebase class , First of all, this class is going to inherit System.Web.UI.Page, which is essentially rewriting a method that involves the lifecycle of the ASPX page and a series of pipe events, which, to put it simply, is equivalent to a filter that when you request another page (to be accessed after landing), Then they all inherit this page, will first in this page to determine whether you have landed, if not landing, then jump to the landing page.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

Main code

protected override void OnInit (EventArgs e)
        {
            if (session["uinfo"] = = null)
            {
                //1. Determine if the user is checked remember three days without landing
                if (request.cookies["UID"]!= null)
                {
                    BLOGUSERBLL BLL = new Bloguserbll ();
                    2. Data entity
                    Bloguser UModel = BLL through the data from the cookie. Getmodel (int. Parse (request.cookies["UID"). Value));
                    3. The entity is deposited in the session
                    session["Uinfo"] = UModel;
                    return;
                }
               4. Jump to landing page
                Response.Redirect ("/login.aspx");
            }
            Base. OnInit (e);
        }

Note: BasePage is for the ASPX page, then ashx page, we will have another parent class, is the Basehandler, the principle is similar, the writing is still a little different, if you are interested can contact me, I can send to you, there is no clear also can leave a message.

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.