2011-04-02 78 views
0

我有以下的UrlRewrite代码,从http更改为https在url中,但现在我需要它从https更改为httpURL重写HTTP到HTTPS,但现在倒退

例如,当我从账户/登录页面重定向到主页时。

这里是我的重写规则:

<rewrite> 
    <rules> 
     <rule name="RequiresHTTPS-Redirect" stopProcessing="true"> 
     <match url="(.+)" /> 
     <conditions> 
      <add input="{HTTPS}" pattern="off" /> 
      <add input="{RequiresHTTPS:{R:1}}" pattern="(.+)" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}/{C:1}" 
       appendQueryString="true" redirectType="Found" /> 
     </rule> 
    </rules> 
    <rewriteMaps> 
     <rewriteMap name="RequiresHTTPS"> 
      <add key="Account/LogOn" value="Account/LogOn" /> 
     </rewriteMap> 
    </rewriteMaps> 
</rewrite> 
+0

用户是否点击登录按钮或通过表单身份验证重定向来保护您使用'web.config'中的''保护的文件夹登录页面 – Kev 2011-04-02 15:19:58

回答

1

有一对夫妇的情景在这里:

1,所有内容由网络保护的形式<authorization>

你的用户在进行登录页面,因为他们已经浏览到该网站的一部分这是使用<authorization>元素保护的。如果是这种情况,您将在查询字符串中传回给您的返回网址:ReturnUrl。您可以将用户重定向回到那里,他们用不来了SSL:

return Redirect("http://" + Request.Url.Host + returnUrl); 

2.用户必须登录到启用其他功能:

您的用户点击登录链接,使一些额外的功能如果您的网页未登录,那么该网页将被裁剪掉。例如,能够发布论坛消息或查看优质内容。

在这种情况下,您可以在登录页面上登录他们之前的位置。本示例基于您在创建新的MVC3应用程序(您可能已将其用作项目模板)时使用Visual Studio 2010获得的模板应用程序。

在该示例应用程序中,每个页面使用主页面Site.MasterSite.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:

我还包含重定向到非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); 
} 

的例子也许有点简单,但你应该能够得到大概的概念。

+0

谢谢队友我可以解决这个问题。 – BigChief 2011-04-03 01:54:01

2

把逻辑回非HTTPS页面重定向在登录页面本身。从https重定向到http的问题在于浏览器在获得重定向之前仍然会首先发起https连接的SSL连接,因此它没有多大意义。

+0

好吧,听起来不错,但现在我需要在每个页面中设置它,因为从登录页面可以访问很多页面? – BigChief 2011-04-02 11:59:00

+0

我用c#使用asp.net mvc 3。你有一些示例代码? – BigChief 2011-04-02 12:15:56