简体   繁体   中英

How to redirect a specific URL to another URL in ASP.NET?

Is it possible to redirect a specific URL to another page?

Suppose a user receives a link eg (mywebsite.com/ConfirmEmail.aspx?with-some-token/code) by clicking on this link/url, it goes only one time to confirm page (mywebsite/ConfirmEmail.aspx) and the confirm page must be opened once with this url which contains token and/or code and the second time if the url (mywebsite.com/ConfirmEmail.aspx?with-some-token/code) is clicked/opened it redirects to home page.

My problem is here... If i remove the token or the code from the URL it still goes to the confirm page which is not allowed. And now I want to redirect only the url mywebsite.com/ConfirmEmail.aspx which does not contain the token and or code to home page (index.aspx).

Here is my code:

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

                if (Request.QueryString["Code"] != null)
                {
                    string id = Request.QueryString["Code"].ToString().Replace(";domain;", "=").Replace(' ', '+');
                    string un = Request.QueryString["un"].ToString().Replace(";domain;", "=").Replace(' ', '+');
                    string emailID = ED.EncryptDycrpt.DecryptString(id, "tokencode");
                    string UserName = ED.EncryptDycrpt.DecryptString(un, "tokencode");
                    lblUserName.Text = UserName;
                    DataTable dt = objBlUser.sp_selectConfirmUserByEmail(emailID);
                    if (dt.Rows.Count > 0)
                    {
                        Response.Redirect("index.aspx");
                    }
                    int rec = objBlUser.Jobs_ConfirmUserEmail(emailID);
                    if (rec > 0)
                    {
                        //Label1.Text = "user confirmed";
                        //send email
                        Sendemail(emailID, UserName);
                    }
                    else
                        Response.Redirect("index.aspx");
                }
            }

        catch (Exception exe)
        {

            Response.Redirect("index.aspx");
        }  
    }

Add the token to the session. Like

Session["Token"] = token;

And when page opens if session["token"] then redirect otherwise execute your code.

Session will clear up if the user leaves the site.

Solution Here!!!

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

                if (Request.QueryString["Code"] != null)
                {
                    string id = Request.QueryString["Code"].ToString().Replace(";domain;", "=").Replace(' ', '+');
                    string un = Request.QueryString["un"].ToString().Replace(";domain;", "=").Replace(' ', '+');
                    string emailID = ED.EncryptDycrpt.DecryptString(id, "tokencode");
                    string UserName = ED.EncryptDycrpt.DecryptString(un, "tokencode");
                    lblUserName.Text = UserName;
                    DataTable dt = objBlUser.sp_selectConfirmUserByEmail(emailID);
                    if (dt.Rows.Count > 0)
                    {
                        Response.Redirect("index.aspx");
                    }
                    int rec = objBlUser.Jobs_ConfirmUserEmail(emailID);
                    if (rec > 0)
                    {
                        //Label1.Text = "user confirmed";
                        //send email
                        Sendemail(emailID, UserName);
                    }
                    else
                        Response.Redirect("index.aspx");
                }
             else
                  {
                        Response.Redirect("index.aspx");
                  }
            }

        catch (Exception exe)
        {

            Response.Redirect("index.aspx");
        }  
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM