Wednesday, October 14, 2026

Cookies login

 

Match login id and password using cookies under 1 minutes if user not input valid I'd pass in 1 min then cookies expired and go to home page in aspx page logic c#

Yes. In ASP.NET Web Forms (.aspx + C#), you can create a temporary login cookie that expires after 1 minute if the user has not successfully logged in. After expiry, redirect the user to the Home page.

1. Login.aspx — C#

protected void btnLogin_Click(object sender, EventArgs e)
{
    string userId = txtUserId.Text.Trim();
    string password = txtPassword.Text.Trim();

    // Match User ID and Password
    if (userId == "admin" && password == "12345")
    {
        // Successful login
        HttpCookie loginCookie = new HttpCookie("LoginUser");

        loginCookie["UserId"] = userId;
        loginCookie["IsLoggedIn"] = "true";

        // Cookie expires after 1 minute
        loginCookie.Expires = DateTime.Now.AddMinutes(1);

        // Security settings
        loginCookie.HttpOnly = true;
        loginCookie.Secure = Request.IsSecureConnection;

        Response.Cookies.Add(loginCookie);

        // Go to dashboard/home after successful login
        Response.Redirect("Home.aspx");
    }
    else
    {
        // Invalid login
        lblMessage.Text = "Invalid User ID or Password.";
    }
}

2. Check cookie on protected pages

For example, in Dashboard.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CheckLogin();
    }
}

private void CheckLogin()
{
    HttpCookie loginCookie = Request.Cookies["LoginUser"];

    if (loginCookie == null ||
        loginCookie["IsLoggedIn"] != "true")
    {
        Response.Redirect("Home.aspx");
        return;
    }
}

3. If you specifically want a 1-minute login attempt window

If your requirement is:

User opens Login page → gets only 1 minute to enter valid ID/password → after 1 minute, cookie expires → automatically goes to Home.aspx.

Then create an attempt cookie when the Login page opens:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        HttpCookie attemptCookie = new HttpCookie("LoginAttempt");

        attemptCookie["Started"] = DateTime.Now.ToString("o");
        attemptCookie.Expires = DateTime.Now.AddMinutes(1);
        attemptCookie.HttpOnly = true;

        Response.Cookies.Add(attemptCookie);
    }
}

Then validate it during login:

protected void btnLogin_Click(object sender, EventArgs e)
{
    HttpCookie attemptCookie = Request.Cookies["LoginAttempt"];

    if (attemptCookie == null)
    {
        Response.Redirect("Home.aspx");
        return;
    }

    DateTime startTime;

    if (!DateTime.TryParse(attemptCookie["Started"], out startTime))
    {
        Response.Redirect("Home.aspx");
        return;
    }

    // Check 1-minute limit
    if (DateTime.Now > startTime.AddMinutes(1))
    {
        Response.Redirect("Home.aspx");
        return;
    }

    string userId = txtUserId.Text.Trim();
    string password = txtPassword.Text.Trim();

    // Match credentials
    if (userId == "admin" && password == "12345")
    {
        // Remove attempt cookie
        HttpCookie expiredCookie = new HttpCookie("LoginAttempt");
        expiredCookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(expiredCookie);

        // Create login cookie
        HttpCookie loginCookie = new HttpCookie("LoginUser");
        loginCookie["UserId"] = userId;
        loginCookie["IsLoggedIn"] = "true";
        loginCookie.Expires = DateTime.Now.AddMinutes(30);
        loginCookie.HttpOnly = true;
        loginCookie.Secure = Request.IsSecureConnection;

        Response.Cookies.Add(loginCookie);

        Response.Redirect("Home.aspx");
    }
    else
    {
        lblMessage.Text = "Invalid User ID or Password.";
    }
}

Important: Cookie expiration alone does not cause an ASPX page to automatically redirect at exactly 1 minute. If you need the browser to automatically redirect to Home.aspx after 60 seconds, use JavaScript/Meta refresh in addition to the server-side cookie check. Also, for a real application, don't store or compare plain-text passwords in code—use a database with hashed passwords.

No comments:

Post a Comment

Cookies login

  Match login id and password using cookies under 1 minutes if user not input valid I'd pass in 1 min then cookies expired and go to hom...