有一对夫妇的情景在这里:
1,所有内容由网络保护的形式<authorization>
:
你的用户在进行登录页面,因为他们已经浏览到该网站的一部分这是使用<authorization>
元素保护的。如果是这种情况,您将在查询字符串中传回给您的返回网址: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.cs
Logon
回发的操作方法可以返回用户到他们的网站上,但使用http
与其使用https:
:
我还包含重定向到非SSL应该有一个returnUrl
通过ASP.NET提供的窗体身份验证:
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);
}
的例子也许有点简单,但你应该能够得到大概的概念。
来源
2011-04-02 16:28:18
Kev
用户是否点击登录按钮或通过表单身份验证重定向来保护您使用'web.config'中的''保护的文件夹登录页面 –
Kev
2011-04-02 15:19:58