views:

175

answers:

1

Account Page opens like normal:

    public ActionResult Index()
    {            
        return View();
    }

this goes to render the following View template: (because at this point there is no CurrentUser, 'user' is null, so it runs Html.RenderAction("LogOn", "Account");)

<% var user = (WAPConfigUser) HttpContext.Current.Session["CurrentUser"]; 
   if (user == null || user.IsLoggedIn == false || user.IsPublic) 
   {
       Html.RenderAction("LogOn", "Account");
   } else { %>

    <span class="PageNavLink">
        <% Html.RenderPartial("AccountMenu"); %>        
    </span>
    <div id="IndexContent" class="IndexContent">
        <% Html.RenderAction("UserList", "Account"); %>    
    </div>

<% } %>

so I type in the username & password, and the following code runs (and the user is added to the Session for retreival later) and I get redirected to the page I want:

(specifically return Redirect(redirect);)

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult LogOn(WAPConfigUser user)
    {
        if (!ValidateLogOn(user.UserName, user.Password))
        {
            return PartialView();
        }
        else
        {
            string redirect = "";
            if (Request.QueryString["ReturnUrl"] != null)
            {
                redirect = Request.QueryString["ReturnUrl"].ToString();
                return Redirect(redirect);
            }
            else
            {
                return RedirectToRoute("Default");
            }
        }
    }

I then log out and I'm redirected to the logon screen:

public RedirectToRouteResult LogOut()
{
    Session.Remove("CurrentUser");
    return RedirectToAction("Index", "Account");
}

When I type my username & password again, the first breakpoint that gets hit is: (which attempts to load the entire page, it seems)

    public ActionResult Index()
    {            
        return View();
    }

the next breakpoint that gets hit is the line return RedirectToRoute("Default");:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult LogOn(WAPConfigUser user)
    {
        if (!ValidateLogOn(user.UserName, user.Password))
        {
            return PartialView();
        }
        else
        {
            string redirect = "";
            if (Request.QueryString["ReturnUrl"] != null)
            {
                redirect = Request.QueryString["ReturnUrl"].ToString();
                return Redirect(redirect);
            }
            else
            {
                return RedirectToRoute("Default");
            }
        }
    }

but if I step through the code my Default route never gets executed. When I step to the next line, it goes to the following line Html.RenderAction("LogOn", "Account"); again in my AccountController's Index View and that (just the user control) gets returned to the browser:

<% var user = (WAPConfigUser) HttpContext.Current.Session["CurrentUser"]; 
   if (user == null || user.IsLoggedIn == false || user.IsPublic) 
   {
       Html.RenderAction("LogOn", "Account");
   } else { %>

    <span class="PageNavLink">
        <% Html.RenderPartial("AccountMenu"); %>        
    </span>
    <div id="IndexContent" class="IndexContent">
        <% Html.RenderAction("UserList", "Account"); %>    
    </div>

<% } %>

There's a few things I don't understand here.

  1. Why is RedirectToRoute("Default") not redirecting me to the default route?
  2. Why is the user null after I added it to the Session when I logged in the second time? (i.e., the line var user = (WAPConfigUser) HttpContext.Current.Session["CurrentUser"]; should return the logged-in user that I just added to the session previous to this. Instead, that line doesn't get hit by the breakpoint so it seems to have another NOT-logged-in user. It's executing a line that it shouldn't be, on a page that it shoudln't be!

I know I'll probably get shot for adding the user to the Session, but does anyone have any idea what's going on? If more info is needed, ask & I'll update the question.

Thanks

Dave

A: 

The problem was:

first it was going to Index & returning a View();

public ActionResult Index()
{            
    return View();
}

That view contained an instruction to call

Html.RenderAction("LogOn", "Account");

...and some other stuff happened that I'm unsure of.

What eventually worked was to take control of the POST with jQuery and to call the LogOn Action Method directly. This bypassed the need for Index(). I did the validating and depending on the result, I redirect the user to the required page.

DaveDev