//先贴出代码protected void btn_login_Click(object sender, EventArgs e)//登录按钮
    {
        if (SignIn.GetUserSignIn(UserName.Text, PassWord.Text)) //判断是否成功登录
        { 
            show();
        }
        else
        {
            Page.RegisterStartupScript("script", "<script language='javascript'>alert('用户名或密码错误!');</script>");
            UserName.Text = "";
        }
    }
    public void show() //成功登录
    {        if (Session["Employee_Name"] != "" && Session["Employee_Name"] != null)
        {
            Name.Text = Session["Employee_Name"].ToString(); // 显示用户名
        }        
        if (Session["Department"] != "" && Session["Department"] != null)
        {
            string DepartmentName = Session["Department"].ToString(); //用户的部门
            Department.Text = DepartmentName.Length > 5 ? DepartmentName.Substring(0, 5) + "..." : DepartmentName;
        }
        if (Session["Manager"] != "" && Session["Manager"] != null)
        {
            Manager.Text = Session["Manager"].ToString(); //用户所属的组
        }
        Panel1.Visible = false;
        Panel2.Visible = true;    }
///////////重要的部分出现了public class SignIn
{    #region【取得登录用户信息】
    public static bool GetUserSignIn(string username, string password)
    {        string strCmd = "select UserID,UserCode,UserName,ManagerGroupID,DepartmentID,DepartmentName,CanLogin,";
        strCmd += "  UserTypeID,CreateTime,NewManagerAction,";
        strCmd += "  GroupID,GroupName,ManagerID,ManagerName,IsBaocanMang,IsBaocanDaiding,NewAction ,ManagerAction, ";
        strCmd += "  NewManagerAction from View_1 where UserCode='" + username.ToString() + "' and CanLogin='1'";
        DataSet ds = Login.ReaderDataSet(Login.createCon(), strCmd);        if (username != "" && username != null)
        {
            if (ds.Tables[0].Rows.Count != 0)
            {
                string UserName = username; //用户名称
                string Domain = "webtest-displays.com";//域的名称
                string Password = password;//用户密码
                UserLoginForDomain CheckUserLogin = new UserLoginForDomain();
                if (CheckUserLogin.impersonateValidUser(UserName, Domain, Password))
                { //一直到这里都没有问题,域验证也是成功的,只有使用session记录状态时,提示错误
   
                    System.Web.HttpContext.Current.Session["LoginSuccess"] = true; //判断用户是否登录 True为登录                    System.Web.HttpContext.Current.Session["Employee_Name"] = ds.Tables[0].Rows[0][2].ToString();//存储用户的姓名
                    return true;
                }
                else
                {
                    System.Web.HttpContext.Current.Session["LoginSuccess"] = false;
                    return false;
                }
            }
            else
            {
                System.Web.HttpContext.Current.Session["LoginSuccess"] = false;
                return false;
            }
        }
        else
            return false;
    }
    #endregion}使用session保存信息,就会产生上面的错误。不使用session就no have Question.本地使用session没有问题,发布成网站,通过iis浏览,就会产生问题。

解决方案 »

  1.   

    System.Web.HttpContext.Current先判断 System.Web.HttpContext.Current 是否为 null再判断 GetUserSignIn 被调用的地方是否可以使用 session有可能 IHttpHandler 的实例未实现 IRequiresSessionState 接口
      

  2.   


    谢谢,正在实验中。前面两个都能理解。
    有可能 IHttpHandler 的实例未实现 IRequiresSessionState 接口 这个不理解,怎么判断呢。
      

  3.   

    if (CheckUserLogin.impersonateValidUser(UserName, Domain, Password))
                    {
                        if (System.Web.HttpContext.Current.Session == null)
                        {
                            Bind.Alert("为空");
                        }
                        else
                        {
                            System.Web.HttpContext.Current.Session["Employee_Name"] = "你好啊!";
                        }
                         return true;
                    }
                    else
                    {
                        System.Web.HttpContext.Current.Session["LoginSuccess"] = false;
                        return false;
                    }
    ///这样写都ok的。
    //Name显示为:你好啊、所以第一个问题和第二个问题都不存在了。
      

  4.   

    有可能 IHttpHandler 的实例未实现 IRequiresSessionState 接口 这个不理解,怎么判断呢。
    -----------------------------------------------------------
    Page 就是一个 IHttpHandler 的实例可以用 is IRequiresSessionState 判断public class SignIn=》public class SignIn : System.Web.SessionState.IRequiresSessionState    但很可能错误的原因不在于此,因为错误提示不一样。
      

  5.   


    是的,问题依旧。
     if (CheckUserLogin.impersonateValidUser(UserName, Domain, Password))
                    {
                        if (System.Web.HttpContext.Current == null)
                        {
                            Bind.Alert("为空");
                        }
                        else
                        {
                            System.Web.HttpContext.Current.Session["LoginSuccess"] = true;
                            System.Web.HttpContext.Current.Session["Employee_EmployeeID"] = ds.Tables[0].Rows[0][0].ToString().Trim();  //用户id
                            System.Web.HttpContext.Current.Session["Employee_Code"] = ds.Tables[0].Rows[0][1].ToString().Trim(); //用户工号
                            System.Web.HttpContext.Current.Session["Employee_Name"] = ds.Tables[0].Rows[0][2].ToString().Trim(); //用户名称
                            System.Web.HttpContext.Current.Session["DepartmentID"] = ds.Tables[0].Rows[0][4].ToString().Trim(); //部门所在ID
                            System.Web.HttpContext.Current.Session["Employee_Department"] = ds.Tables[0].Rows[0][5].ToString().Trim(); //用户部门
                            System.Web.HttpContext.Current.Session["Employee_CanLogin"] = ds.Tables[0].Rows[0][6].ToString().Trim(); //是否可以登陆                        System.Web.HttpContext.Current.Session["Employee_GroupID"] = ds.Tables[0].Rows[0][10].ToString().Trim(); // 课别ID
                            System.Web.HttpContext.Current.Session["Employee_GroupName"] = ds.Tables[0].Rows[0][11].ToString().Trim(); // 课别名称
                            System.Web.HttpContext.Current.Session["Employee_UserTypeID"] = ds.Tables[0].Rows[0][12].ToString().Trim(); // UserTypeID 权限
                            System.Web.HttpContext.Current.Session["Employee_UserTypeName"] = ds.Tables[0].Rows[0][13].ToString().Trim(); //用户权限(管理员,用户管理员)
                            System.Web.HttpContext.Current.Session["IsBaocanMang"] = ds.Tables[0].Rows[0][14].ToString().Trim(); // 报餐权限
                            System.Web.HttpContext.Current.Session["IsBaocanDaiding"] = ds.Tables[0].Rows[0][15].ToString().Trim(); //是否为代订员
    ////上面的我去掉注视没有问题////下面是我新添加的,去掉注释就和上面的问题一样
                            System.Web.HttpContext.Current.Session["UserPwd"] = password.ToString();
                            System.Web.HttpContext.Current.Session["ManagerID"] = ds.Tables[0].Rows[0][12].ToString(); //ManagerID
                            System.Web.HttpContext.Current.Session["Manager"] = ds.Tables[0].Rows[0][13].ToString();//用户所在组名                        System.Web.HttpContext.Current.Session["UserCode"] = ds.Tables[0].Rows[0][1].ToString();//工号
                            System.Web.HttpContext.Current.Session["UserName"] = ds.Tables[0].Rows[0][2].ToString();//姓名
                            System.Web.HttpContext.Current.Session["DepartmentID"] = ds.Tables[0].Rows[0][4].ToString();//部门ID
                            System.Web.HttpContext.Current.Session["Department"] = ds.Tables[0].Rows[0][5].ToString();//部门名称                        System.Web.HttpContext.Current.Session["GroupID"] = ds.Tables[0].Rows[0][10].ToString();//课别ID
                            System.Web.HttpContext.Current.Session["Group"] = ds.Tables[0].Rows[0][11].ToString();//课别名称                        System.Web.HttpContext.Current.Session["ManagerAction"] = ds.Tables[0].Rows[0][17].ToString();//拥有的后台权限
                            System.Web.HttpContext.Current.Session["NewAction"] = ds.Tables[0].Rows[0][16].ToString();//是否有新增的权限
                            System.Web.HttpContext.Current.Session["NewManagerAction"] = ds.Tables[0].Rows[0][18].ToString();//新增的后台权限
                            System.Web.HttpContext.Current.Session["UserID"] = ds.Tables[0].Rows[0][0].ToString();//用户所在Users表中的id
                        }
                         return true;
                    }
                    else
                    {
                        System.Web.HttpContext.Current.Session["LoginSuccess"] = false;
                        return false;
                    }版主可能疏忽一条信息,本地测试完全可以,发布之后,通过iis浏览就像上面一样。
      

  6.   

    if (CheckUserLogin.impersonateValidUser(UserName, Domain, Password))
                    {能够进到这个if 中是么?
      

  7.   


    可以的,是返回True的。我现在不能理解的是:
    我新建了一个页面里面只有两个textbox和一个button,点击button调用的方法是一样的,这样他就没有问题。
    我现在正在使用的页面,里面是完整的网站前台页面,可能一些东西和session冲突,貌似不存在吧。
    目前调式也不好调式,前台页面中的内容都快被我删没了,可依旧是不行,问题依旧存在。
      

  8.   

    你这个问题和session 没关系,可能是你还没找到真正出问题的地方