Asp.net mvc 3 为什么';是否将此加载到https?
在asp.net mvc 3中,我有一个站点具有ssl证书,并且在https中运行良好。唯一公开的页面是登录页面。无论出于何种原因,它都不会以https加载。这是我的相关代码(如果我遗漏了什么,我会根据要求发布更多) web.configAsp.net mvc 3 为什么';是否将此加载到https?,asp.net-mvc-3,https,requirehttps,requiressl,Asp.net Mvc 3,Https,Requirehttps,Requiressl,在asp.net mvc 3中,我有一个站点具有ssl证书,并且在https中运行良好。唯一公开的页面是登录页面。无论出于何种原因,它都不会以https加载。这是我的相关代码(如果我遗漏了什么,我会根据要求发布更多) web.config <compilation debug="false" targetFramework="4.0"> <authentication mode="Forms"> <forms loginUrl="~/Account/LogO
<compilation debug="false" targetFramework="4.0">
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" requireSSL="true"/>
</authentication>
账户控制员
#if !DEBUG
[RequireHttps]
#endif
public class AccountController : Controller
{
public ActionResult LogOn()
{
return View();
}
}
加载登录视图时,它不在Https中。我错过了什么?您需要添加
[RequireHttps]
,所以它看起来像这样:
[RequireHttps]
public ActionResult LogOn()
{
return View();
}
这将迫使它使用Https
编辑
也许您需要将以下内容添加到Web.Config
的
部分:
<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>
您需要将构建目标设置为在构建站点时发布。您将在Visual Studio中看到如下下拉列表,将其更改为“发布并重建”,然后发布您的站点:
我知道这是个愚蠢的问题,但你是在调试模式下运行吗?@JohnH-:)没有(或者至少我相当确定没有),请参阅web.config中包含的行<代码>啊,是的,对不起,我错过了。:)@TravisJ-web.config条目与编译站点时定义的调试符号无关。这是在Visual Studio的生成目标设置中设置的。@MystereMan-啊,你是对的!为了使#Debug在发行版中为false,我应该编辑什么?操作方法不是从控制器继承需求吗?是的,它们继承了,如果删除
#if!调试
?对不起,我没注意到。我认为可以使用localhost:44300
在HTTPS
中进行调试。奇怪的是,删除\IF
指令可以使[RequireHttps]
正常工作。什么会导致IF失败?我猜正确的URL没有被重写。尝试将我的编辑添加到您的Web.Config
文件中,看看是否有帮助。我不确定,因为我从未在这种情况下使用过#If
。@TravisJ你知道你说的是#If!调试
,所以您应该只希望release在HTTPS
中运行,否则您应该使用#if DEBUG
进行调试。但我怀疑这是你的问题。太棒了!这很有效,谢谢你。我不敢相信我错过了这么简单的东西。@TravisJ-请记住,MVC是一个Web应用程序项目,这意味着所有代码都在编译时编译成dll。网页(aspx或cshtml)仍然是在运行时编译的,但是.cs文件不是,因此您必须将生成目标设置为release以获得真正的发布生成。谢谢,我确实注意到在部署bin时.dll大小的差异。
<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>