ASP.NET MVC Ajax message for action requiring authentication returns login view when user session timed out

I am using Ajax.BeginForm to create a form that will do ajax postback for a specific controller action and then the response view will be inserted into the UpdateTargetId.

using (Ajax.BeginForm("Save", null,
        new { userId = Model.UserId },
        new AjaxOptions { UpdateTargetId = "UserForm" },
        new { name = "SaveForm", id = "SaveForm" }))
   {
    [HTML SAVE BUTTON]
   }

      

Everything works fine except when the user's session has been disconnected and then redirected back to the login page. Then the login page is returned from the Ajax call due to the Authorize attribute and the html is loaded in the UpdateTargetId and so I end up with the login html page in the user profile page (in the target id). My controller action looks like this:

[Authorize]
public ActionResult Save(Int32 UserId)
{
    //code to save user
    return View("UserInfoControl", m);

}

      

How can I solve this problem?

UPDATE (2011-10-20): Found this post from Phil Haack about this exact issue - http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when- you-donrsquot-want.aspx . I haven't gotten a chance to digest or implement his solution yet.

+2


source to share


1 answer


I think you can deal with the authorization issue inside the activity. First remove [Authorize], use this code:

public ActionResult Save(Int32 UserId)
{
    if (!User.Identity.IsAuthenticated) 
    { 
        throw new Exception();
    }
    //code to save user
    return View("UserInfoControl", m);

}

      



Then you can put an OnFailure condition in your AjaxOptions and redirect your page or something.

0


source







All Articles