WEB页面统一认证登录方法


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

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

 1 None.gif using  System;
 2 None.gif using  System.Web;
 3 None.gif using  System.Web.Handlers ;
 4 None.gif
 5 None.gif namespace  WebApplication1
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 8InBlock.gif    /// LoginModule 的摘要说明。
 9ExpandedSubBlockEnd.gif    /// </summary>

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

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

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

42ExpandedSubBlockEnd.gif            }

43InBlock.gif            catch (Exception ex)
44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
45InBlock.gif                throw ex;
46ExpandedSubBlockEnd.gif            }

47ExpandedSubBlockEnd.gif        }

48InBlock.gif
49InBlock.gif        public void Init(HttpApplication context)
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
52ExpandedSubBlockEnd.gif        }

53InBlock.gif
54InBlock.gif        public void Dispose()
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56InBlock.gif            
57ExpandedSubBlockEnd.gif        }

58ExpandedSubBlockEnd.gif    }

59ExpandedBlockEnd.gif}

60 None.gif

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

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

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

转载于:https://www.cnblogs.com/CrazyWill/archive/2005/11/07/270838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值