gpt4 book ai didi

.net - 对 mvc 4 View 的子集使用 https 端点

转载 作者:行者123 更新时间:2023-12-02 07:15:45 26 4
gpt4 key购买 nike

我正在使用 Azure 上托管的 mvc 4 构建 Web 解决方案。来自 winforms/wpf 世界,我意识到对于 Web 配置还有很多我不了解的地方。我也被视为 MVC 新手,所以我可能会忽略这里明显的一些东西......

在我的 Azure Web 角色中,我配置了两个端点,一个用于 http,一个用于 https。我想要实现的是,将一组公共(public) View / Controller 绑定(bind)到我的 http 端点,并将一组 ssl 保护的 Controller 绑定(bind)到我的 https 端点。

例如

http://www.mysite.com/home

https://www.mysite.com/login

https://www.mysite.com/account

我想我正在寻找一种将 Controller 绑定(bind)到配置的端点的方法,或者如果这是不可能的,请确保 http://www.mysite.com/login被重定向到https://www.mysite.com/login ,对于未 protected Controller 反之亦然。

我的解决方案还包含一组 mvc 4 webapi Controller ,这些 Controller 都受 ssl 保护。如果无法避免在 http 端点上暴露这些内容,那么如果有人尝试在不安全的端点上访问这些内容,我想用 http 错误代码进行响应。

我确信我可以通过编程方式解决这个问题,但是有没有一种干净的方法可以通过配置来解决这个问题?

<小时/>

作为奖励,像这样设置它也会很酷:

https://secure.mysite.com/login

我通过 setting up two mvc 4 projects 实现了这一点,定义两个<Site> s 在服务配置文件中。但是,我不想将其分成两个项目。这可以通过某种配置来实现吗?

为了给您提供一些可以使用的东西,这里是我的 ServiceConfiguration.csdef 的摘录

 <Sites>
<Site name="www" physicalDirectory="..\project1">
<Bindings>
<Binding name="HttpIn" endpointName="HttpIn" />
</Bindings>
</Site>
<Site name="api" physicalDirectory="..\project2">
<Bindings>
<Binding name="HttpsIn" endpointName="HttpsIn" hostHeader="secure.mysite.com" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="HttpsIn" protocol="https" port="443" certificate="Certificate1" />
<InputEndpoint name="HttpIn" protocol="http" port="80" />
</Endpoints>

最佳答案

您通常会使用 RequireHttpsAttribute为此(将此应用于您只想通过 HTTPS 访问的任何 Controller 或操作,例如 AccountController)。 Carlos Figueira 还拥有 ASP.NET Web API 实现 on his blog .

关于.net - 对 mvc 4 View 的子集使用 https 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12023426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com