.NET BasePage 登陆判断基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace
{
    public class BasePage : System.Web.UI.Page
    {
        //用于为子类继承,主要考虑权限,通用内容
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            if (System.Web.HttpContext.Current != null)
            {
                int bz = 1;
                if (Session["RoleName"] != null && Session["RoleName"].ToString() != "sa")
                {
                    bz = CheckUser();
                }
                if (Session["EmployeeID"] == null)
                {
                    //Response.Redirect("login.aspx");
                    //ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToString(), "parent.window.location.href('login.aspx');", true);
                    if (bz == 0)
                    {
                        Response.Write("<script language = javascript>alert('当前用户已在其他地方登录,您将被退出系统!');</script>");
                    }

                    Response.Write("<script language = javascript>window.parent.location.href='Login.aspx'</script>");
                    Context.ApplicationInstance.CompleteRequest();  //跳过所有的管线链中的事件。
                    Response.Flush();  //向客户端发送你的输出流
                    Response.End();//终止当前线程

                }
                base.OnInit(e);
            }

        }

        private int CheckUser()
        {
            int bz = 0;
            if (HttpContext.Current.Session["EmployeeID"] != null)
            {
                string myGuid = HttpContext.Current.Session["userguid"].ToString();
                string myUserID = HttpContext.Current.Session["EmployeeID"].ToString();

                System.Collections.Hashtable hs = (System.Collections.Hashtable)Application["userList"];

                foreach (System.Collections.DictionaryEntry de in hs) //ht为一个Hashtable实例 
                {
                    if (de.Key == myUserID && de.Value == myGuid)
                    {
                        bz = 1;
                        break;
                    }
                    //Console.WriteLine(de.Key);//de.Key对应于key/value键值对key 
                    //Console.WriteLine(de.Value);//de.Key对应于key/value键值对value 
                }
            }
            else    //session失效,当为真,不经过此定时判断来跳转
            {
                bz = 2;
            }
            if (bz == 0 || bz == 2)
            {
                Session["EmployeeID"] = null;
            }
            return bz;
        }

        public void AddScript()
        {
            Page.ClientScript.RegisterClientScriptInclude("myMessage", "./Scripts/jquery-1.4.1.js");
            string script = "<script language=\"JavaScript\"> ";
            script += " function myrefresh() ";
            script += " { ";
            //script += " $.ajax({  ";
            //script += "     url: 'GetData.ashx', ";
            //script += "     type: 'post',  ";
            //script += "    dataType: 'html', ";
            //script += "     data: { type: 'TR' },";
            //script += "    error: function () { ";
            //script += "        alert('Error'); ";
            //script += "    }, ";
            //script += "    success: function (msg) {  ";
            //script += "        if (msg != \"\") { ";
            //script += "           if(msg==\"0\") ";
            //script += "           { ";
            //script += "            alert('当前用户已在其他地方登录,您将被退出系统!'); ";
            //script += "           } ";
            //script += "       } ";
            //script += "    } ";
            //script += "   }); ";
            script += " $.getJSON(\"TR.aspx\", function (data) { ";
            script += "       if(data == \"1\") ";
            script += "       {";
            script += "          alert('当前用户已在其他地方登录,您将被退出系统!'); ";
            script += " window.parent.location='Login.aspx'; ";
            script += "       } ";
            script += "      });";

            script += " } ";
            script += " setInterval('myrefresh()',6000);";
            script += "  </script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "tr", script);
            //ScriptManager.RegisterStartupScript(UpdatePanel, this.GetType(), "InitQuickCheckBtn", "<script>setRunQuickCheckBtn();</script>", false);  
        }
    }
}

  

posted @ 2014-07-25 15:09  feimon  阅读(236)  评论(0)    收藏  举报