You can use the returned URL completely through the SignOn process using the query string of the request.
First, indicate the page you want to return to whenever you produce a partial SignOn sign:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="Microsoft.Web.Mvc"%>
<% if (!User.IsAuthenticated) %>
<%= Html.RenderAction("SignOn", "Account", new { returnUrl = Request.Url.PathAndQuery }); %>
Use RenderAction if the current context is not an Account Controller. This feature is currently not available in the MVC release, so you need to include the ASP.NET MVC Future library in your solution.
Next, the SignOn controller:
public ActionResult SignOn(string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
User user = userRepository.GetItem(u => u.aspnet_UserName == User.Identity.Name);
return !string.IsNullOrEmpty(returnUrl)
? Redirect(returnUrl)
: (ActionResult) RedirectToAction("Index", "Home");
}
return PartialView();
}
SignOn Form:
<% using (Html.BeginForm("SignOn", "Account", new { returnUrl = Request.QueryString["returnUrl"] },FormMethod.Post,new {@class="my_signOn_class"}))
{ %>
<% } %>
Finally, in the SignOn controller that processes the POST form, you can return the user to "returnURL" using the following code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SignOn(FormCollection formCollection, string returnUrl)
{
if (BusinessRuleViolated(formCollection))
{
if(!string.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");
}
}
source
share