Link to home
Create AccountLog in
Avatar of nirsait
nirsait

asked on

Redirect to original page from login page

I have some code that is working to a point. Basically I want our users to come to the site and click a members only link, if they are already logged in the code will verify that they are able to access the page if they don't have the correct access they will go to an error page. If they have access they move to the members only page.

The next part is where I am having troubles. If the user isn't logged on I want to redirect them to the login page but after they login I need to send the back to the original page they came from. I have attached the code that I am working with I think I just need to be looking at how I can append the returl to the end of the response.redirect but I am not a .NET programmer so this stuff is new to me.

<script language="C#" runat="server">
    protected override void OnPreRender(EventArgs e)
    {
        if (Request.QueryString["iErrorType"].ToString() == "Asi.Security.AccessDenied")
        {
                if (Asi.Security.AppPrincipal.CurrentIdentity.IsAuthenticated && Asi.Security.AppPrincipal.CurrentIdentity.LoginUserId != "")
                {
                    /* we are already logged in so we must be non member */
                    Response.Redirect("http://www.mysite.org/error.aspx");
                }
                else
                {

                    Response.Redirect("http://www.mysite.org/login/login.aspx?retUrl=%2fwcm%2fHome.aspx%3fhkey%3d24b324f6-f50f-4902-bbff-afd780040e26%26WebsiteKey%3d75dea602-e911-478d-b061-91876fecc73d");
                }
        }
           
        base.OnPreRender(e);

    }

</script>

Open in new window

Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

You appear to have already appended the the return url to the redirect e.g.

?retUrl=%2fwcm%2fHome.aspx%3fhkey%3d24b324f6-f50f-4902-bbff-afd780040e26%26WebsiteKey%3d75dea602-e911-478d-b061-91876fecc73d

Open in new window


All you should need is some code in your login page after a successful login which calls:

Response.Redirect(Request.QueryString["returl"]);

Open in new window


Although thats not the safest code in the book, you would ideally want to add some checks to that to ensure you are redirecting to your own site and not a phishing site.
Avatar of nirsait
nirsait

ASKER

You are correct. I do have a return url hardcoded in right now so that users go somewhere after logging in. Mainly I wanted to work on getting people to the login page after they clicked on the members only link. Now that I have it going to the login page I need to get them back to the page they were originally trying to access that required the user be logged in.

I tried the code you provided but I think the problem is that I don't have access to editing the login page. I only have the ability to change the code that I included above which actually is an error page. Our system was throwing an error if the user was logged in so I made code changes to the page so that instead of just giving the user an error message they get routed to the login page if they aren't logged in.
If you can't edit the login page, you can't make it redirect after login? Am I missing something?
Avatar of nirsait

ASKER

No that sounds right but I was hoping somehow that I would be able to edit the page and store the variable to call later. Since I am able to have the redirect hard coded in the code I didn't know what other options I had with storing the page they are trying to hit.

I will need to have the developers of the site get in and do some looking now.
ASKER CERTIFIED SOLUTION
Avatar of nirsait
nirsait

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of nirsait

ASKER

Was able to find a workable solution with some added code.