Redirect to Original Page After Login

Jul 26, 2011 at 7:44 AM

Trying to do the following, and not sure why I'm having trouble...

Original Page -> Begin Login Action -> Twitter Auth -> Complete Login Action -> Original Page

Basically my attempt was to attach the referring Url to the login using:


Couldn't get it to work... not sure where it goes in the Auth process...

Is there an example anywhere of redirecting back to a specific page after the user logs in successfully?

Jul 26, 2011 at 3:08 PM

Hi mmezzacca,

The downloadable code has a LinqToTwitterWebFormsDemo project.  The Default.aspx has a demo for WebAuthorizer and SignIn.aspx has a demo for SignInAuthorizer.  They both use Request.Url in the BeginAuthorization call to specify the callback.


Jul 26, 2011 at 3:47 PM


Thanks for your reply.  I have seen those examples and implemented it.  However, at the time the user logs in, the Request.Url is the Account/Login page (Login ActionResult).  Once they sucessfully login it currently sends them to the homepage.  Instead I'd like to send the user back to where they were prior to that.  I could use cookies, but I was wondering if there was a way to send it through the OAuth login?  Here is the code I have currently:

                Uri callbackUrl = Request.Url;
                if (Request.UrlReferrer != null)
                    callbackUrl = new Uri(Request.Url.ToString() + "?redirectUrl=" + HttpUtility.UrlEncode(Request.UrlReferrer.ToString()));

                auth = new MvcAuthorizer
                    Credentials = credentials,
                    Callback = callbackUrl


                if (!auth.IsAuthorized)
                    return auth.BeginAuthorization();
But the redirectUrl isn't kept through the process...

Aug 2, 2011 at 3:39 AM

Any thoughts on this?

Aug 2, 2011 at 3:54 AM

You're using MVC?


Aug 2, 2011 at 4:14 AM


Aug 2, 2011 at 4:25 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Aug 2, 2011 at 4:39 AM

I think I see what the problem is now.  The callback needs to be set during BeginAuthorization, but the code didn't have a way to do that.  However, now I've fixed that and here's how you can set the URL:

Uri specialUri = new Uri(Request.Url.ToString() + "?redirectUrl=" + HttpUtility.UrlEncode(Request.UrlReferrer.ToString()));
return auth.BeginAuthorization(specialUri);

I've overloaded BeginAuthorization to allow setting the URL.  The non-parameterized overload defaults to Request.Url.  You can find the new code at Change Set 91526.


Aug 2, 2011 at 5:29 AM

Thanks Joe, really appreciate the quick fix!  I'll check out the latest change set.

- Marc