惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
H
Help Net Security
博客园 - 叶小钗
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
月光博客
月光博客
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
Vercel News
Vercel News

博客园 - CrazyWill

VBscript(ASP)计算程序运行时间,精度到毫秒 VBscript(ASP)常用函数 301永久重定向实现方式及302重定向 开源搜索引擎资源 18种方法让你集中精力工作 IE的BUG - CrazyWill - 博客园 弱智的卡巴死机 bash比较文件时间和当前时间 - CrazyWill - 博客园 老问题: SQL server 2000安装过程中错误解决 使用TELNET手工操作 IMAP 查看邮件 安装 samba 记录 使用TELNET手工操作 SMTP/POP 收发邮件 SQL语句实现横排 2 世界 Web 2.0 网站评奖揭晓 MagicAjax使用简介 [链接]什么是Web 2.0——下一代软件的设计模式和商业模式 SQL语句实现合并数据(原创) SQL语句实现横排 一道有趣的C#考试题目
WEB页面统一认证登录方法
CrazyWill · 2005-11-07 · via 博客园 - CrazyWill


在每一个页面的aspx.cs文件中都要写代码检查用户是否已登录,麻烦。这样子来做比较好。

先建一个测试用解决方案吧,WebApplication1
新建一个类,就叫LoginModule好了,继承 IHttpModule 接口,内容差不多是这样子:

 1using System;
 2using System.Web;
 3using System.Web.Handlers ;
 4
 5namespace WebApplication1
 6{
 7    /// <summary>
 8    /// LoginModule 的摘要说明。
 9    /// </summary>

10    public class LoginModule : IHttpModule 
11    {
12        public LoginModule()
13        {
14            //
15            // TODO: 在此处添加构造函数逻辑
16            //
17        }

18
19        private void context_AcquireRequestState(object sender, EventArgs e)
20        {
21            HttpContext context = HttpContext.Current;
22            try
23            {
24                //把不需要验证的Handler加在这里
25                if (context.Handler is TraceHandler)
26                {
27                    return;
28                }

29                if(context.Session["Session_User"== null)
30                {
31                    string curUrl = context.Request.Url.ToString();
32
33                    string currUser = "";
34                    // 用户登录检查代码
35                    // .
36
37                    if(currUser == null || currUser.Trim().Length == 0)
38                        throw new Exception("你还不是本系统的用户,如果需要进行本系统的操作,请联系管理员");
39                    else
40                        context.Session["Session_User"= currUser;
41                }

42            }

43            catch (Exception ex)
44            {
45                throw ex;
46            }

47        }

48
49        public void Init(HttpApplication context)
50        {
51            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
52        }

53
54        public void Dispose()
55        {
56            
57        }

58    }

59}

60

然后再在 Web.Config 中增加如下设置。

<configuration>
  
<system.web>
    
<httpModules>
        
<add name="LoginModule" type="WebApplication1.LoginModule, WebApplication1" />
    
</httpModules>
 
</system.web>
</configuration>

这样就把登录认证的操作统一在LoadModule中进行处理啦。省掉好多工作量 :)