How Do I redirect if a page is not accessed from Default.aspx?

I have a confirmation page which receives a querystring with a reference number, I dont want the users open this page at all if they are not redirected form the first page (default.aspx) I tried the code below but this would redirect in all cases,,,
public partial class Confirmation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //Preventing Direct Access to the page
       
            if (Request.UrlReferrer == null )
            {
                Response.Redirect("Default.aspx");
            }


            if (Request.UrlReferrer.AbsolutePath != "~/Default.aspx")
            {
                Response.Redirect("Default.aspx");
            }


            if (!IsPostBack)
        {

            //Label1.Text = DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm");
            //  Label1.Text = (string)Foo.GetNextValue();
            // Label1.Text = Foo.GetNextValue().ToString();
            // lblConfirmation.Text = Foo.GetNextValue().ToString();
            // Label1.Text = Request["value"].ToString();
            Label1.Text = Server.UrlDecode(Request.QueryString["value"].ToString());

Open in new window

LVL 9
TonyRebaAsked:
Who is Participating?
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Yes! There is another way. Use querystring checking as in code:

//Preventing Direct Access to the page
        if (Request.QueryString["refNumber"] != null)
        {
            // Do something here
        }
        else
        {
            Response.Redirect("Default.aspx");
        }

Open in new window


If querystring is null user will be redirected to Default.aspx page simply.

Let us know if that solved problem or not.
0
 
Kumaraswamy RCommented:
in the Web.config:

<customErrors defaultRedirect="ErrorPage.asp&shy;x" mode="On">
    <error statusCode="401" redirect="default.aspx" />      
</customErrors>

more
http://www.codeproject.com/KB/aspnet/Custon401Page.aspx
http://stackoverflow.com/questions/3552848/asp-net-forms-authentication-redirects-to-myapp-default-aspx-instead-of
0
 
PryratesCommented:
you can additionally try
Request.Headers("referer")

Open in new window


I would always check with
.EndsWith("Default.aspx");

Open in new window

insteas of the whole path.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
sybeCommented:
If you use Webforms, then you can use IsPostBack
0
 
TonyRebaAuthor Commented:
Hi Sybe I am using IspostBack but I dont know where should the code blockl would be placed.
0
 
sybeCommented:
if (!IsPostBack)
            {
                Response.Redirect("Default.aspx");
            }
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hi TonyReba ,

After reading your problem scenario and testing it on development PC I found some code error (mentioned below).

Understanding Scenario First:
Your website named Shopping (for understanding, read it accordingly yours) has two pages named Default.aspx and Confirmation.aspx.

Default.aspx have single button to redirect user to Confirmation.aspx programatically.

And I used same above code in Confirmation.aspx.cs's Page_Load, I found that the following code line is wrong, not truly wrong but used in wrong way:

if (Request.UrlReferrer.AbsolutePath != "~/Default.aspx")
            {
                Response.Redirect("Default.aspx");
            }

Open in new window



Why? Because Request.UrlReferrer.AbsolutePath returns path as /Shopping/Default.aspx which would never match string value "~/Default.aspx".

Solution Rectify above code as:

 if (Request.UrlReferrer.AbsolutePath != "/Shopping/Default.aspx")
        {
            Response.Redirect("Default.aspx");
        }

Open in new window


Note: Change Shopping name in above code with your site name.
0
 
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Sorry for above wrong solution's code as doing things speedily makes them wrong. Rectify code as:

if (Request.UrlReferrer.AbsolutePath.Equals("/Shopping/Default.aspx"))
        {
            // Do something here    
        }
        else
        {
            Response.Redirect("Default.aspx");
        }

Open in new window

0
 
TonyRebaAuthor Commented:
Is there another way to check to prevent direct access to this confirmation page? The thing is this seems I will only work for a webserver and project folder name.?
0
 
TonyRebaAuthor Commented:
Cool. that was more what I was looking for, I never thought about it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.