views:

117

answers:

5

I have two pages, NonMember.aspx and Member.aspx. If a user comes to the site, they will go to NonMember.aspx as soon as they login, I want them to immediately be redirected to Member.aspx, but instead of doing this, it is staying on NonMember.aspx. The user actually has to go click on the menu item again to get to Member.aspx.

The links are located at http://abc.com/tools/NonMember.aspx and http://abc.com/tools/Member.aspx.

I was doing:

System.IO.Path.GetFileNameWithoutExtension(Request.Url.ToString());

but I am thinking there is a better way, especially since I have multiple default.aspx pages and this could pose a problem

Here is more detail on what exactly I am doing:

When I run the site on my local development machine, the NonMember page points to:

http://testserver/tools/NonMember.aspx.

Requet.Url.AbsolutePath points to /testserver/tools/NonMember.aspx.

Then I am doing this:

if(url == "~/tools/NonMember.aspx")
{
    Response.Redirect("~/tools/Member.aspx");
}

The above is not working and I can check if url is equal to /testserver/tools/NonMember.aspx because if I deploy to liveserver, it will fail.

A: 

If you have the HttpResponse object, you can use HttpResponse.Redirect

Grzenio
I have the redirect working from the updated example above, but there has to be a better way since I can have multiple pages called default.
Xaisoft
A: 

You should check your cookie or session variable to see if the user is logged in and then use a Response.Redirect to move them to the member page.

Totty
I have the authentication part down, it is what is the best way to get the url of the current page the user is on.
Xaisoft
Maybe you should try System.Web.HttpContext.Current.Request.Url.AbsolutePath
Totty
I updated my question with what you suggested.
Xaisoft
+1  A: 

When using Forms Authentication for an ASP.NET application, it will automatically redirect you to the page you were viewing before you logged in. This is why you are redirected back to the NonMember.aspx page.

It would be better if you had just one member page, and perform a check in the page to see if the user is authenticated, if so, display the member content, otherwise, display the non-member content.

Then, when the user logs in and is redirected back to the page, they will see the member content.


If you are insistent on keeping the two separate pages, then in your check you simply have to see if the current user is authenticated (through the IsAuthenticated property on the User that is exposed through the page) and then redirect to your members page. If you are on the NonMember page, you don't need to check to see what the url is (unless this is MVC, which you didn't indicate).

casperOne
I updated my question.
Xaisoft
Non MVC. Does the testserver and livserver part make sense.
Xaisoft
@Xaisoft: I don't see how it matters, since you don't need to check the URL at all given my response.
casperOne
I don't want it to redirect me to the page when before I logged on. The member should be redirected to the Member page from the NonMember page as soon as they logon.
Xaisoft
A member on ~/tools/NonMember.aspx who signs on should be redirected to ~/tools/Member.aspx. The problem I have is that if I use AbsoluteUrl and I get /testserver/tools/NonMember.aspx and if I try to compare this like so:if(url == "/testserver/tools/NonMember.aspx"), it will work, but as soon as the server changes from testserver to liveserver, then I will have the problem, so I really want to do something like:if(url == "~/tools/NonMember.aspx"), but this does not work.
Xaisoft
@Xaisoft: You don't have to do the URL check at all, simply check in the NonMember.aspx page if the user is authenticated. If they are, issue a redirect to the Member page. No URL checking is needed, so the live/test server issue is moot.
casperOne
I tried doing if User.Identity.IsAuthenticated, but it stays false even during the login. so I am trying to handle it during the Login Button click event and that is why I am trying to compare.
Xaisoft
I am checking if they are authenticated in the Page_Load and to be more clear, Members have access to the Non Member page, so they can go to that page, but if the sign in, they should be redirected to the Member page.
Xaisoft
A: 

I'm not sure I get your scenario but, let's try.

If you have public pages, the login page and private pages (member pages) and once you authenticate your users you want them to browse ONLY the private part of your website, you should check early in the processing steps (AuthorizeRequest would be a good phase) if the request comming in is for a public or for the login page which you also consider public and redirect to your private page from there (like...having a "home" page for the member area of the site and always redirecting to it once you get an authenticated and properly authorized request to some public URL of your site).

EDIT: You should check for Request.FilePath

FilePath returns /testserver/tools/NonMember.aspx which would break once I deploy this to /liveserver/tools/NonMember.aspx.
Xaisoft
so that's a folder... then define some global string and assign it testserver in debug and liveserver in release.
A: 

I ended up just doing the following:

if(Authenticated)
{
    string path = Request.UrlReferrer.AbsolutePath;

    if (path.EndsWith("/tools/NonMember.aspx"))
    {
        Response.Redirect("~/tools/Member.aspx");
    }
}
Xaisoft