FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

Last post 05-29-2008 12:41 AM by Paul Linton. 6 replies.

Sort Posts:

  • FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

    05-28-2008, 5:32 AM
    • Member
      18 point Member
    • hnchan
    • Member since 08-10-2007, 1:48 AM
    • Posts 119

    Hi All

    How could I implement FormsAuthentication.RedirectFromLoginPage  under MVC Preview 3 since every function needs to return the Actionresult.

    Regards

    Alex

  • Re: FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

    05-28-2008, 7:04 AM
    • All-Star
      60,921 point All-Star
    • anas
    • Member since 09-21-2006, 8:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,865
    • Moderator
  • Re: FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

    05-28-2008, 10:54 AM
    • Member
      18 point Member
    • hnchan
    • Member since 08-10-2007, 1:48 AM
    • Posts 119

    But under MVC Preview 3, it has ActionResult return type under the controller, how could I handle for FormsAuthentication.RedirectFromLoginPage. Could I make the FormsAuthentication.RedirectFromLoginPage as the ActionResult?

  • Re: FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

    05-28-2008, 1:30 PM

    There isn't any special type of ActionResult for forms authentication. Your simplest solution is either to have a "void" action method, or just to call RedirectFromLoginPage, then return null, ie.

     

    public ActionResult MyActionMethod() 
    {
    	if(loginIsValid)
    	{
    		FormsAuthentication.RedirectFromLoginPage(... etc ...);
    		return null;
    	}
    	else
    	{
    		// Do whatever else
    		return RedirectToAction("Login");
    	}
    }
     

    If  you're bothered about testability, you can create your own special ActionResult (FormsAuthenticationActionResult?) whose Execute method calls FormsAuthentication.RedirectFromLoginPage.

  • Re: FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

    05-28-2008, 2:03 PM
    • All-Star
      60,921 point All-Star
    • anas
    • Member since 09-21-2006, 8:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,865
    • Moderator

    You can avoid using FormsAuthentication.RedirectFromloginPage ,

    Instead , use FormsAuthentication.SetAuthCookie(UserName,IsPersisted) ,which just set the authentication cookie without redirection ,

     then redirect to the action that you want

     

    public ActionResult MyActionMethod() 
    {
    	if(loginIsValid)
    	{
    		FormsAuthentication.SetAuthCookie(username....
    		return RedirectToAction("ActionName");
    	}
    	else
    	{
    		// Do whatever else
    		return RedirectToAction("Login");
    	}
    }

     

    Right ? Sad

    Regards,

    Anas Ghanem | Blog

  • Re: FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

    05-29-2008, 12:02 AM
    • Member
      18 point Member
    • hnchan
    • Member since 08-10-2007, 1:48 AM
    • Posts 119

    But if the user doesn't login through the main page and he needs to return to his previous page, is it possible to handle it?

     

  • Re: FormsAuthentication.RedirectFromLoginPage and MVC Preview 3

    05-29-2008, 12:41 AM
    • Contributor
      5,076 point Contributor
    • Paul Linton
    • Member since 04-29-2008, 11:16 PM
    • Posts 883

    There are a few pieces to the puzzle.  When your LogOn action is called the first time the Forms Authentication mechanism will have added a query string parameter called returnUrl.  You need to capture this.  Something like

    public ActionResult LogOn(string returnUrl)
    {
       ViewData["ReturnUrl"] = returnUrl;
       return View();
    }

     Your view needs to have a hidden field so that the returnUrl can be passed back to the authentication action.  Somewhere inside the form which collects username and password (in the case above it would be called LogOn.aspx) you will want

    <%= Html.Hidden("returnUrl", ViewData["ReturnUrl"]) %>

    Assuming the the LogOn form submits to an action called Authenticate your code will be

     

    public ActionResult Authenticate(string username, string password,
                                     string retuUrl)
    {
       if (ValidLogin(username, password) {
          FormsAuthentication.SetAuthCookie(username, false);
          if (returnUrl == null)
             return RedirectToAction("someAction", "someController");
          else
             return Redirect(returnUrl);
       }
       else
       {
          return RedirectToAction("LogOn", new {returnUrl = returnUrl});
       }
    }
     There are probably neater ways of doing this, but it works for me.
    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
Page 1 of 1 (7 items)