views:

318

answers:

2

When I get AuthenticationStatus.Authenticated (DotNetOpenId library) response from myopenid provider, i'd like to redirect user from login page to another one using MVC Redirect(myurl). But unfortunately, instead of getting to myurl, user is redirected to empty page:

myurl?token=AWSe9PSLwx0RnymcW0q.... (+ several kilobytes of myopenid-specific query string)

I also tried FormsAuthentication.RedirectFromLoginPage(), but it redirects to original login page again instead of the myurl.

Could anybody suggest proper redirection to myurl?

Thanks

+1  A: 

First of all you should set authorization cookie:

FormsAuth.SetAuthCookie(UserName, RememberMe);

After this you should return RedirectToAction result:

return RedirectToAction(actionName, controllerName);

Or Redirect result:

return Redirect(url);

When you use "return Redirect(url);" it uses Response.Redirect(url, false) method for redirecting so it must work. May be you not returning this action result, please check this. "FormsAuthentication.RedirectFromLoginPage()" will redirect to login page when you didn't set authorization cookie or when your login page is a home (default) page of the application -

The RedirectFromLoginPage method redirects to the URL specified in the query string using the ReturnURL variable name. For example, in the URL http://www.contoso.com/login.aspx?ReturnUrl=caller.aspx, the RedirectFromLoginPage method redirects tothe return URL caller.aspx. If the ReturnURL variable does not exist, the RedirectFromLoginPage method redirects to the URL in the DefaultUrl property.

Via MSDN

zihotki
A: 

I use the classic ReturnUrl querystring parameter to get the user back to the right page. Unfortunately, the RedirectFromLoginPage does not work well after OpenId Authentication so you must do it manually. Note, this is done as an authentication module rather than deeper in on the controller. It feels cleaner this way.

      FormsAuthentication.SetAuthCookie(openid.Response.ClaimedIdentifier, false);
      //FormsAuthentication.RedirectFromLoginPage(openid.Response.ClaimedIdentifier, false); <-- doesn't work
      //send back to the right page
      string returnUrl = ctx.Request.QueryString["ReturnUrl"];
      if (!string.IsNullOrEmpty(returnUrl))
      {
        returnUrl = HttpUtility.UrlDecode(returnUrl);
        ctx.Response.Redirect(returnUrl);
      }

If you'd like to see the entire implementation of the OpenIdAuthenticationModule, check out the source code on codeplex.

JarrettV