My problem is for postItem.aspx and giveComment.aspx, users cannot access without login.
Can i use the nested-if method? Then how to use it? Thank you...

In Login.aspx.cs

 if (temp == 1)
        {
        string cmdStr2 = "Select Password from Reg where Username= '" + txtUN.Text + "'";
        SqlCommand pass = new SqlCommand(cmdStr2, con);
        string Password = pass.ExecuteScalar().ToString();
        con.Close();

        if (Password == txtPass.Text)
        {
            if("current page"==postItem.aspx){
            Session["New"] = txtUN.Text;
            Response.Redirect("postItem.aspx");
        }
        }

        if(Password == txtPass.Text)
        {
            if("current page"==giveComment.aspx){
            Session["New"] = txtUN.Text;
            Response.Redirect("giveComment.aspx");
        }
        }

In postItem.aspx.cs

 if (Session["New"] != null)
        {
            Label1.Text += Session["New"].ToString();

        }

        else
        {
            Response.Redirect("Login.aspx");

        }

In giveComment.aspx.cs

 if (Session["New"] != null)
        {
            Label1.Text += Session["New"].ToString();

        }

        else
        {
            Response.Redirect("Login.aspx");

        }

**I want to make it likes if users now in postItem.aspx and without login then after he/she login then it will redirect to postItem.aspx.
On the hands, if users now in giveComment.aspx and without login then after he/she login then it will redirect to giveComment.aspx.

Thank you... :)

Dani AI

Generated

— the typical pattern is to send the user to the login page with a ReturnUrl (the page they were trying to open) and, after a successful login, redirect back to that ReturnUrl. Comparing literal strings like "current page" == postItem.aspx won’t work and duplicates your login logic. pointed you in the right direction; here’s a concrete, safe way to implement it.

On each page that requires authentication, check the session and redirect to Login.aspx with the current URL encoded:

if (Session["Username"] == null)
{
    string returnUrl = Server.UrlEncode(Request.RawUrl);
    Response.Redirect("~/Login.aspx?returnUrl=" + returnUrl);
}

On Login.aspx, after you verify credentials, set the session and redirect to the returnUrl if it is present and local:

protected void btnLogin_Click(object sender, EventArgs e)
{
    if (ValidateCredentials(txtUN.Text, txtPass.Text)) // use secure check
    {
        Session["Username"] = txtUN.Text;
        string returnUrl = Request.QueryString["returnUrl"];
        if (!String.IsNullOrEmpty(returnUrl) && IsLocalUrl(returnUrl))
            Response.Redirect(returnUrl);
        else
            Response.Redirect("~/Default.aspx");
    }
}

Helper to avoid open-redirects:

private bool IsLocalUrl(string url)
{
    return !String.IsNullOrEmpty(url) && url.StartsWith("/") && !url.StartsWith("//");
}

Security notes (important): never build SQL with string concatenation — use parameterized queries. Do not store plain-text passwords; store salted hashes (PBKDF2/BCrypt) and verify properly. If you prefer a built-in approach, configure Forms Authentication in web.config and call FormsAuthentication.RedirectFromLoginPage to reuse the framework’s ReturnUrl handling. Avoid Request.UrlReferrer — it can be null or spoofed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.