asp.net BasePage类+Session通用用户登录权限控制
但是很多人都喜欢在
protected void Page_Load(object sender, EventArgs e) {}
里面来写代码,甚至在某些按钮里面写判断session是否存在~~
这样当然是能实现效果的,问题就在,如果有1000个页面~~你需ctrl+C。。。Ctrl+V 很多次~~~
我的思路就是写一个BasePage类继承 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)); }}
为什么我这里要带 Page 参数,就是为了在登录成功以后可以返回到登录前的那一个页面
另外我也贡献一个SessionData类:
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
更多asp.net BasePage类+Session通用用户登录权限控制相关文章请关注PHP中文网!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

C#是一种现代、面向对象的编程语言,由微软开发并作为.NET框架的一部分。1.C#支持面向对象编程(OOP),包括封装、继承和多态。2.C#中的异步编程通过async和await关键字实现,提高应用的响应性。3.使用LINQ可以简洁地处理数据集合。4.常见错误包括空引用异常和索引超出范围异常,调试技巧包括使用调试器和异常处理。5.性能优化包括使用StringBuilder和避免不必要的装箱和拆箱。

C#.NET应用的测试策略包括单元测试、集成测试和端到端测试。1.单元测试确保代码的最小单元独立工作,使用MSTest、NUnit或xUnit框架。2.集成测试验证多个单元组合的功能,常用模拟数据和外部服务。3.端到端测试模拟用户完整操作流程,通常使用Selenium进行自动化测试。

C#.NET依然重要,因为它提供了强大的工具和库,支持多种应用开发。1)C#结合.NET框架,使开发高效便捷。2)C#的类型安全和垃圾回收机制增强了其优势。3).NET提供跨平台运行环境和丰富的API,提升了开发灵活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#高级开发者面试需要掌握异步编程、LINQ、.NET框架内部工作原理等核心知识。1.异步编程通过async和await简化操作,提升应用响应性。2.LINQ以SQL风格操作数据,需注意性能。3..NET框架的CLR管理内存,垃圾回收需谨慎使用。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在企业级应用、游戏开发、移动应用和Web开发中均有广泛应用。1)在企业级应用中,C#常用于ASP.NETCore开发WebAPI。2)在游戏开发中,C#与Unity引擎结合,实现角色控制等功能。3)C#支持多态性和异步编程,提高代码灵活性和应用性能。

C#在.NET中的编程过程包括以下步骤:1)编写C#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。
