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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
sybeCommented:
If you use Webforms, then you can use IsPostBack
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TonyRebaAuthor Commented:
Cool. that was more what I was looking for, I never thought about it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.