views:

3747

answers:

3

This is probably one of those easy questions.. I'm trying to redirect the user after they've successfully authenticated, or return them back to the login page. But the Success page is on a different route and I can't get the redirection to work..

Here are my routes in Globals.asax:

routes.MapRoute( _
    "Default", _
    "{controller}/{action}/{id}", _
    New With {.controller = "Login", .action = "Index", .id = ""} _
    )
routes.MapRoute( _
    "Stuff", _
    "{controller}/{action}/{id}", _
    New With {.controller = "Stuff", .action = "Index", .id = ""} _
    )

I've got 2 Controllers: LoginController.vb and StuffController.vb. The Views/Login/Index.aspx file contains a simple form with the code:

<form method="post" action="/Login/Authenticate">

The LoginController contains the following code:

Function Authenticate() As RedirectToRouteResult
    ' authentication code commented out  ;o)

    Return RedirectToRoute("Stuff")
End Function

And the StuffController contains the following:

Function Index()
    ' show stuff..

    Return View()    ' return /Views/Stuff/Index.aspx
End Function

Here's what I've tried so far:

  • Function Authenticate()
  • Function Authenticate() As ActionResult()
  • Function Authenticate() As RedirectToRouteResult()

all of which cause a Redirect Loop timeout in the browser. What am I missing?!

A: 

I fail to see where you are setting the authentication cookie or marking the user as having been authenticated in any way. Is that in the code that you have omitted?

tvanfosson
Yes, I may be wrong but I didn't think I'd need any authentication code as all I'm really trying to do here is 'jump' Controllers - from the LoginController to the StuffController
Andrew
If you don't set the auth. cookie, provided your action methods are decorated with the [Authorize] attribute, then you will be redirected back to your login url again.
liggett78
Thanks for the note about the [Authorize] attribute. Truth is when I was trying to get this working I hadn't written the authorisation code so I hadn't decorated any Action Methods with [Authorize] - I was just getting the 'flow' right to start with :o)
Andrew
+2  A: 

Could it be that your Stuff route has exactly the same form as the default one, so when you call

Return RedirectToRoute("Stuff");

the resulting url has the form: {controller}/{action}/{id}, e.g. Login/Authenticate again, since you are inside Login-controller's Authenticate action.

Try to

RedirectToAction("Index", "Stuff");

Hope that helps.

liggett78
You're correct. I removed the 'Stuff' Route, and used the RedirectToAction("[View]", "[Controller]") and it works - thanks for your help :o)
Andrew
Oh, and I needed to change the Return Type of the Authenticate Function to "ActionResult" too :o)
Andrew
A: 

try

routes.MapRoute( _    
"Stuff", _
"",_  
New With {.controller = "Stuff", .action = "Index", .id = ""} _    
)