views:

34

answers:

1

I have two types of roles [Admin, HelpDeskAdmin].

I have a single logon view(both users go to same link to login) and I want to check their role once logged in and redirect to their respective admin pages once authenticated. The code below doesn't identify the logged in user as being in the role the first time and reloads the logon page, the second time they logon it redirects correctly.

Does this have something to do with the authentication cookie not being in place when it checks the first time? How could I accomplish this scenario?

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
  if (MembershipService.ValidateUser(model.UserName, model.Password))
  {
     FormsService.SignIn(model.UserName, model.RememberMe);
     if (!String.IsNullOrEmpty(returnUrl))
     {
         return Redirect(returnUrl);
     }
     else
     {
         if (Roles.IsUserInRole("Admin"))
         {
              //go to admin landing page
              return RedirectToAction("Index", "Manage"); 
         }
          else if (Roles.IsUserInRole("HelpDesk"))
          {
              //go to helpdesk landing page
              return RedirectToAction("Index", "Interview"); 
          }
          else /******FIRST TIME THROUGH IT ALWAYS GOES HERE *******/
             return RedirectToAction("Index", "Home");  //not in any of those roles
      }
}
else
     {
       ModelState.AddModelError("", "The user name or password provided is incorrect.");
      }
}
 // If we got this far, something failed, redisplay form
return View(model);
} 
A: 

The user is technically NOT logged in until the next request is handled and the Authenication Cookie is set....

Either do the redirection logic in another Action method or perhaps extract the user info using Roles.IsUserInRole(model.UserName, "Admin") [[Note specifying the username]] from within this Action.

JcMalta
OK thanks, I moved the redirect logic into a LoginRedirect Action method that I redirect to once the login is validated.
jrob