zoukankan      html  css  js  c++  java
  • ASP.NET 实现登陆验证

     public class ValidModule : IHttpModule
        {
            /// <summary>
            /// 您将需要在网站的 Web.config 文件中配置此模块
            /// 并向 IIS 注册它,然后才能使用它。有关详细信息,
            /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
            /// </summary>
            #region IHttpModule Members
    
            public void Dispose()
            {
                //此处放置清除代码。
            }
    
            public void Init(HttpApplication context)
            {
                try
                {
                   context.AcquireRequestState += new EventHandler(OnLoginRequest);
                }
                catch
                {
    
                }
            }
    
            #endregion
    
            public void OnLoginRequest(Object source, EventArgs e)
            {
               
                HttpApplication application = (HttpApplication)source;
                if (application.Context.Session!=null && application.Context.Session["Loginer"] == null)
                {
                    string requestUrl = application.Request.Url.ToString();
                    string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);
                    if (!requestPage.Contains("Login.aspx"))
                    {
                        application.Response.Redirect("~/Login.aspx");    
                    }
                }
            }
        }
    View Code

    有两点说明:

    1.Session在这个事件阶段存在 context.AcquireRequestState += new EventHandler(OnLoginRequest);

    2.  if (application.Context.Session!=null && application.Context.Session["Loginer"] == null)判断,原先JS、CSS都被过滤掉了,有时还会有

    ‘未将对象引用到实例’,找了半天,加了第一个判断。这个验证还不周全,还应该有登陆后验证,防止登陆用户访问非法页面。

    想来无心一笔,后觉惊鸿一笔。O(∩_∩)O,代码呀,呵呵。

  • 相关阅读:
    BFS 路径记录
    KMP算法
    STL标准库-容器-deque 双端队列
    4. 位运算(快速幂)
    词频统计(未完成,错误)
    字符串转整形
    AVL平衡二叉树的各种问题(Balanced Binary Tree)
    string用scanf读入printf输出(节省时间)
    【转】Java魔法堂:String.format详解
    【转】关于Android资源文件中出现百分号的问题
  • 原文地址:https://www.cnblogs.com/aswater-yuanye/p/3494668.html
Copyright © 2011-2022 走看看