ASP. BasePage class +session Universal User Login Rights control

Source: Internet
Author: User
But many people like to

protected void Page_Load (object sender, EventArgs e) {}

Inside to write code, even in some buttons to write to determine whether the session exists ~ ~
This certainly can achieve the effect, the problem is, if there are 1000 pages ~ ~ You need to CTRL + C ... Ctrl + V very many times ~ ~
My idea is to write a basepage class inheritance System.Web.UI.Page

The public class BasePage:System.Web.UI.Page {//pageunload event does not mean that the browser is closed, but the page is closed, so the following event is still executed when the refresh occurs protected void Page_ Unload (object sender, EventArgs e) {} protected override void Onpreinit (EventArgs e) {base. Onpreinit (e); if (! Sessiondata.islogin ()) {//write here to jump to landing page: for example: Response.Redirect (String. Format ("~/relogin.aspx? Page={0} ", Request.path)); }}

Why do I have to bring the page parameter here, in order to return to the page before login after successful login
In addition I also contribute a Sessiondata class:

Using System; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using Expressplatform.common; Namespace ExpressPlatform.Web.AppCode {public class SessionKey {Public Const string UserInfo = "user";}//<summary  >////data in all sessions, in this category management///</summary> public class Sessiondata {///<summary>///For user information in session// </summary>//<returns></returns> public static Mdlsessioncustomerinfo GetUserInfo () { Mdlsessioncustomerinfo userInfo = Sessionmanager<mdlsessioncustomerinfo>. Getsessionobject (Sessionkey.userinfo); if (UserInfo = = null) {UserInfo = new mdlsessioncustomerinfo ();//Store the content in the application sessionmanager<mdlsessioncustomerinfo& gt;. Setsessionobject (Sessionkey.userinfo, UserInfo); } return userInfo; }///<summary>//Reset user information in session///</summary>//<param name= "UserInfo" ></param> public s tatic void Setuserinfo (Mdlsessioncustomerinfo userInfo) {sessionmanager<mdlsessioncustomerinfo>.Setsessionobject (Sessionkey.userinfo, UserInfo); }///<summary>///Clear Session User Information///</summary> public static void Clearuserinfo () {Sessionmanager<mdlse Ssioncustomerinfo>. Setsessionobject (sessionkey.userinfo, NULL); }///<summary>//whether login///</summary>//<returns></returns> public static bool IsLogin () {Boo l RET = false; Mdlsessioncustomerinfo userInfo = Sessionmanager<mdlsessioncustomerinfo>. Getsessionobject (Sessionkey.userinfo); if (userInfo! = null) ret = TRUE; return ret; } } }
public class BasePage:System.Web.UI.Page 
Related Article

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.