【问题标题】:Using session for user authentication in asp.net c#在asp.net c#中使用会话进行用户身份验证
【发布时间】:2011-12-13 06:43:37
【问题描述】:

我正在使用会话对用户进行身份验证。我的项目中有 2 个网页。一个是webform,一个是EntryForm.aspx,另一个是log.aspx

在 log.aspx 中我已经完成了

protected void Button1_Click(object sender, EventArgs e)
{
        user_login loginu = new user_login();
        String uid_db = loginu.login(this.DropDownList1, this.TextBox1, this.TextBox2, this.Label5);
        if (uid_db == "invalid")
        {
            Label5.Visible = true;
            Label5.Text = "Invalid Login";
        }
        else
        {

            string uname = uid_db.Substring(0, uid_db.IndexOf(",")).Trim();
            string[] tokens = uid_db.Split(',');
            string dbname = tokens[tokens.Length - 1];

            Session["login"] = uname;
            Session["db"] = dbname;
            Response.Redirect("EntryForm.aspx");
       }
}

user_login 类中,我正在获取存储在数据库中的密码并将其与用户输入的值匹配。如果它找到一个值,我将它重定向到 EntryForm.aspx。我在其中检查会话变量如下

protected void Page_Load(object sender, EventArgs e)
    {// CHEK SESSION VARIABLE AND LOAD dropdownlist1 WITH VALUES
        if (!IsPostBack)
        {
            String DB = "";
            String AccountID = "";
            if (Session["login"] != null && Session["db"] != null)
            {
                AccountID = Session["login"].ToString();
                DB = Session["db"].ToString();

                Label9.Text = AccountID;
            }
            else
            {
                Response.Redirect("log.aspx");
            }
            HiddenField1.Value = DB.ToString();
            DropDown a = new DropDown();
            a.filldropdown1(this.DropDownList1, DB);
        }
    }

这就是我所做的验证用户的工作。在服务器上我做了以下配置:

我没有在Global.asax 中进行任何设置,web.config 也没有任何设置。我看过很多论坛,其中配置了Global.asaxweb.config

我想知道在我的项目中我需要做什么才能非常高效地工作。我面临会话超时问题。我在我的服务器上将它设置为 20 分钟,但有时我会突然退出。

请帮助我理解使用会话进行身份验证。

【问题讨论】:

    标签: asp.net session windows-server-2008


    【解决方案1】:

    首先你必须编辑 web.config 并设置会话超时属性。

    <configuration>
      <system.web>
         <sessionState timeout="200"></sessionState>
      </system.web>
    </configuration>
    

    另一个问题是 IsPostBack 块的使用。

    protected void Page_Load(object sender, EventArgs e)
        { 
         if (Session["login"] != null && Session["db"] != null)
          {
             String DB = "";
             String AccountID = "";
             AccountID = Session["login"].ToString();
             DB = Session["db"].ToString();
             Label9.Text = AccountID;
             HiddenField1.Value = DB.ToString();
             DropDown a = new DropDown();
             a.filldropdown1(this.DropDownList1, DB);
           }
         else
         {
             Response.Redirect("log.aspx");
          }
       }
    

    【讨论】:

    • 非常感谢,我想知道我做错了什么以及服务器上有关会话的配置是否正确。
    • @Ishan - 在您的 code-sn-p 中,IsPostBack 块中的语句将在第一页加载时执行。但是,您可以在检查会话属性的 if 正文中检查 IsPostBack 属性。
    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2023-01-26
    • 2022-01-16
    • 1970-01-01
    • 2014-06-16
    • 2012-03-26
    • 2015-05-18
    相关资源
    最近更新 更多