Home >Backend Development >C#.Net Tutorial >asp.net BasePage class +Session universal user login permission control

asp.net BasePage class +Session universal user login permission control

高洛峰
高洛峰Original
2017-01-12 14:03:101160browse

But many people like to write code in

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

, and even write code in some buttons to determine whether the session exists~~
Of course this can achieve the effect, the problem is, if There are 1000 pages~~You need ctrl+C. . . Ctrl+V many times~~~
My idea is to write a BasePage class that inherits System.Web.UI.Page

public class BasePage : System.Web.UI.Page 
{ 
//pageunload事件,并不是指浏览器关闭,而是指页面关闭,所以刷新的时候,依然会执行以下事件 
protected void Page_Unload(object sender, EventArgs e) 
{ 
} 
protected override void OnPreInit(EventArgs e) 
{ 
base.OnPreInit(e); 
if (!SessionData.IsLogin()) 
{//这里写 跳转到登陆页面:例如: 
Response.Redirect(string.Format("~/ReLogin.aspx?Page={0}", Request.Path)); 
}}

Why do I need to bring the Page parameter here, just to return after successful login? Go to the page before login
In addition, I also contributed 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> 
/// 所有session中的数据,在该类管理 
/// </summary> 
public class SessionData 
{ 
/// <summary> 
/// 获取session 中的 用户信息 
/// </summary> 
/// <returns></returns> 
public static MdlSessionCustomerInfo GetUserInfo() 
{ 
MdlSessionCustomerInfo userInfo = SessionManager<MdlSessionCustomerInfo>.GetSessionObject(SessionKey.UserInfo); 
if (userInfo == null) 
{ 
userInfo = new MdlSessionCustomerInfo(); 
//把内容储存到应用程序 
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo); 
} 
return userInfo; 
} 
/// <summary> 
/// 重新设置session 中的用户信息 
/// </summary> 
/// <param name="userInfo"></param> 
public static void SetUserInfo(MdlSessionCustomerInfo userInfo) 
{ 
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, userInfo); 
} 
/// <summary> 
/// 清楚session中用户信息 
/// </summary> 
public static void ClearUserInfo() 
{ 
SessionManager<MdlSessionCustomerInfo>.SetSessionObject(SessionKey.UserInfo, null); 
} 
/// <summary> 
/// 是否登入 
/// </summary> 
/// <returns></returns> 
public static bool IsLogin() 
{ 
bool ret = false; 
MdlSessionCustomerInfo userInfo = SessionManager<MdlSessionCustomerInfo>.GetSessionObject(SessionKey.UserInfo); 
if (userInfo != null) 
ret = true; 
return ret; 
} 
} 
}
public class BasePage : System.Web.UI.Page

For more asp.net BasePage class + Session universal user login permission control related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn