【问题标题】:Session exception in Asp.net website developmentAsp.net 网站开发中的会话异常
【发布时间】:2015-06-16 02:41:03
【问题描述】:
  //************ Try Block ************************************//
sqlStr = "select LoginId from users where LoginID like '" + sUserName.Trim() + "' and CurrentPassword like '" + objFun.EncryptData(sPassword.Trim()) + "'";
                    string sDbUserName = objFun.RunSelectQueryAndReturnString(sqlStr);
                    if (sUserName.ToUpper().Trim() == sDbUserName.ToUpper().Trim())
                    {
                        sqlStr = "select CurrentStatus from users where LoginID like '" + sUserName.Trim() + "' and CurrentPassword like '" + objFun.EncryptData(sPassword.Trim()) + "'";
                        int iStatus = Convert.ToInt32(objFun.RunSelectQueryReturnFirstFieldOfFirstRecord(sqlStr));
                        if (iStatus != 1)
                        {
                            Response.Cookies.Add(new HttpCookie("UserName", sUserName));


                            Session["LoginID"] = sUserName;    // From this line my Code goes to Catch  Block

                        Session["LoginTime"] = DateTime.Now;
                        sqlStr = "";
                        sqlStr = "Update Users set CurrentStatus=1 where LoginID like '" + sUserName + "'";
                        objFun.RunSelectQuery(sqlStr);
                        if (Login1.RememberMeSet == true)
                        {
                            Response.Cookies.Add(new HttpCookie("UserNameHistory", Login1.UserName));
                        }
                        sqlStr = "";
                        objFun.GenerateLog("USERLOGIN", "Logged in into Website", sUserName, 2, 0);
                        Response.Redirect("Home.aspx");// ,true  );

                        //throw new Exception("Unable to redirect");
                    }
                    else
                    {
                        throw new Exception("User is already logged in");
                    }

///****************************Catch Block********************************/

       catch (Exception ex)
        {
            Login1.FailureText = ex.Message;              
        }

Exception I got at RUNTIME:
 {System.Web.HttpException: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.
   at System.Web.UI.Page.get_Session()
   at Login.Login1_Authenticate(Object sender, AuthenticateEventArgs e) 

【问题讨论】:

  • 您是否启用了错误跟踪中指出的会话状态?到目前为止,您还尝试过其他任何事情吗?
  • 是的,我启用了以下方式:在 Login.aspx 页面中
  • 您是否启用了 SessionStateModule 或在 web.config 中提供了自定义?
  • 发布您的网络配置,@David W 是正确的
  • 无法在此评论框中发布 Web.Config 但我的 web.Config 文件中没有任何 EssionStateModule

标签: asp.net session


【解决方案1】:

确保您的 web.config 中存在以下内容,以便为您的应用提供 SessionState:

<system.webServer>
       <modules runAllManagedModulesForAllRequests="true">
        <remove name="Session" />
        <add name="Session" type="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
</system.webServer>

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 2012-09-27
    • 2010-12-22
    • 2014-10-27
    • 2016-05-12
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多