【问题标题】:Why doesn't this load into https?为什么这不加载到 https 中?
【发布时间】:2012-08-10 20:28:26
【问题描述】:

在 asp.net mvc 3 中,我有一个具有 ssl 证书的站点,并且在 https 中运行得很好。唯一暴露的页面是登录页面。无论出于何种原因,它都不会在 https 中加载。这是我的相关代码(如果我遗漏了一些内容,将根据要求发布更多内容)。

web.config

<compilation debug="false" targetFramework="4.0">
<authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn" timeout="2880"  requireSSL="true"/>
</authentication>

global.asax

public static void RegisterRoutes(RouteCollection routes)
{
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
 );
}

帐户控制人

#if !DEBUG
    [RequireHttps]
#endif
public class AccountController : Controller
{
 public ActionResult LogOn()
    {
        return View();
    }
}

登录视图加载时,它不在 Https 中。我错过了什么?

【问题讨论】:

  • 愚蠢的问题我知道,但你是在调试模式下运行吗?
  • @JohnH - :) 不(或者至少我相当肯定不是),看到 web.config 中包含的行了吗? &lt;compilation debug="false" targetFramework="4.0"&gt;
  • 啊,是的,对不起,我错过了。 :)
  • @TravisJ - web.config 条目与编译站点时定义的 DEBUG 符号无关。这是在 Visual Studio 的构建目标设置中设置的。
  • @MystereMan - 啊,你是对的!为了使#Debug 在发布中为假,我应该编辑什么?

标签: asp.net-mvc-3 https requirehttps requiressl


【解决方案1】:

您需要在构建站点时将构建目标设置为发布。您将在 Visual Studio 中看到如下所示的下拉菜单,将其更改为 Release and rebuild,然后发布您的网站:

【讨论】:

  • 太棒了!这行得通,谢谢。我不敢相信我错过了这么简单的事情。
  • @TravisJ - 请记住,MVC 是一个 Web 应用程序项目,这意味着所有代码在编译时都编译为 dll。网页(aspx 或 cshtml)仍然在运行时编译,但 .cs 文件不是,因此您必须将构建目标设置为发布才能获得真正的发布构建。
  • 谢谢,我确实注意到 bin 部署时 .dll 大小有所不同。
【解决方案2】:

你需要添加[RequireHttps],所以它看起来像这样:

[RequireHttps]
public ActionResult LogOn()
{
    return View();
}

这将强制它使用Https。

编辑

也许您需要将以下内容添加到您的Web.Config 的&lt;system.webServer&gt; 部分:

<rewrite>
  <rules>
    <rule name="Secure Account Controller" enabled="true" stopProcessing="true">
      <match url="^account" ignoreCase="true" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
        <add input="{HTTPS}" pattern="off" />
        <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
      </conditions>
      <action type="Redirect" url="https://{C:1}:44300{URL}" />
    </rule>
  </rules>
</rewrite>

【讨论】:

  • 动作方法不继承控制器的需求吗?
  • 是的,如果您删除 #if !DEBUG 会发生什么?抱歉,我没有注意到这一点。我想你可以使用localhost:44300 在HTTPS 中调试。
  • 奇怪的是,删除#IF 指令使[RequireHttps] 功能正常。什么会导致 IF 失败?
  • 我的猜测是正确的 URL 没有被重写。尝试将我的编辑添加到您的 Web.Config 文件中,看看是否有帮助。我不确定,因为在这种情况下我从未使用过#If。
  • @TravisJ 你知道你在说#if !DEBUG,所以你应该期望只有发布在HTTPS 中运行,否则你应该使用#if DEBUG 进行调试。但是我怀疑这是你的问题。
猜你喜欢
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多