这里有几个场景:
1.所有内容均受网络表单保护<authorization>:
您的用户正在访问登录页面,因为他们浏览了使用<authorization> 元素保护的网站部分。如果是这种情况,您将在查询字符串中收到一个返回 URL:ReturnUrl。您可以使用以下方法将用户重定向回没有 SSL 的来源:
return Redirect("http://" + Request.Url.Host + returnUrl);
2。用户必须登录才能启用其他功能:
您的用户正在单击登录链接以启用一些额外功能,如果他们未登录,这些功能会在您的页面上被删除。例如,能够发布论坛消息或查看高级内容。
在这种情况下,您可以跟踪他们在登陆登录页面之前的位置。此示例基于您在创建新的 MVC3 应用程序时使用 Visual Studio 2010 获得的模板应用程序(您可能已将其用作项目的模板)。
在该示例应用程序中,每个页面都使用母版页Site.Master。 Site.Master 执行 Html.RenderPartial("LogOnUserControl") 以在每个页面上呈现登录链接。打开LogOnUserControl.ascx并将呈现登录ActionLink的代码更改为:
else
{
if(!Request.RawUrl.Contains("/Account/LogOn"))
{
Session["WhereWasI"] = Request.Url.AbsoluteUri;
}
%>
[ <%: Html.ActionLink("Log On", "LogOn", "Account") %> ]
<%
}
如果用户未登录,我们基本上是在跟踪用户所在的页面。因为登录链接也呈现在登录页面本身,我们需要排除它,因此if 声明:
if(!Request.RawUrl.Contains("/Account/LogOn"))
然后在您的AccountController.csLogon 回发操作方法中,您可以将用户返回到他们在网站上的位置,但使用http 而不是https::
如果 ASP.NET 表单身份验证提供 returnUrl,我还包括重定向到非 SSL:
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
//
// 1. All content is protected by web forms `<authorization>`:
// If there was a return URL then go back there
//
if(!String.IsNullOrWhiteSpace(returnUrl))
{
return Redirect("http://" + Request.Url.Host + returnUrl);
}
}
else
{
//
// 2. Users have to logon to enable additional features:
//
if (Session["WhereWasI"] != null)
{
return Redirect(
Session["WhereWasI"].ToString().Replace("https", "http"));
}
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("",
"The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
这个例子可能有点简单,但你应该能够大致了解。