【问题标题】:How to use Timeout in ASP.NET application?如何在 ASP.NET 应用程序中使用超时?
【发布时间】:2016-09-28 08:01:20
【问题描述】:

我正在 ASP.NET 中创建登录页面,我想在 3 次尝试失败后阻止用户,并在 10 分钟后取消阻止他。我没有使用Login Control,所以不能使用Membership Provider,所以想到了使用Timeout。如何修改下面的代码来屏蔽和解除屏蔽用户?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Replicon.Cryptography.SCrypt;

namespace WebApplication1
{
    public partial class SignIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public class LoginAttempt {  public DateTime AttemptTime {get;set;} }
        protected void Button1_Click(object sender, EventArgs e)
        {
            String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {

                SqlCommand cmd = new SqlCommand("select * from Users where Username=@Username", con);
                cmd.Parameters.Add("@Username", Username.Text);
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        if (Replicon.Cryptography.SCrypt.SCrypt.Verify(Password.Text, (string)row["Password"]))
                        {  
                            Session["USERNAME "] = Username.Text;
                            Response.Redirect("~/UserHome.aspx");
                            return;
                        }

                        { lblError.Text = "Invalid Username or Password !"; }
                    }
                }


            }
        }


    }
}

【问题讨论】:

    标签: asp.net login timeout scrypt


    【解决方案1】:

    你可以使用会话 如果您遇到错误,您需要在全局 asax 中初始化会话值

    if(Session["DateTime"]==null)
    {
    String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
                using (SqlConnection con = new SqlConnection(CS))
                {
    
                    SqlCommand cmd = new SqlCommand("select * from Users where Username=@Username", con);
                    cmd.Parameters.Add("@Username", Username.Text);
                    con.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
    
    
    
    
    
                   if (dt.Rows.Count != 0)
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            if (Replicon.Cryptography.SCrypt.SCrypt.Verify(Password.Text, (string)row["Password"]))
                            {  
                                Session["USERNAME "] = Username.Text;
                                Response.Redirect("~/UserHome.aspx");
                                return;
                            }
    
                            { lblError.Text = "Invalid Username or Password !"; }
                        }
                    }
    else
    {
    if(Convert.ToInt32(Session["errorlogin"])>3)
    {
        Session["DateTime"]=DateTime.Now;
    }
    Session["errorlogin"]=Convert.ToInt32(Session["errorlogin"])+1;
    
    
    }
    }
    }
    

    【讨论】:

    • 在 Global.asax 中,我应该修改什么?
    • 在 Session_Start 初始 Session["DateTime"] 和 Session["errorlogin"]
    • 用什么值初始化它们?
    • 我尝试了上面的代码,它阻止了所有用户。我该怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多