如何使用ASP.NET MVC 2 Preview 2 Futures RequireHttps属性?
我想防止将不安全的HTTP请求发送到操作方法.我想自动重定向到HTTPS.
MSDN:
我该如何使用此功能?
我正在使用[RequireHttps]强制用户仅在https上浏览特定页面.这很好用,但它有一个问题.一旦用户导航到另一个页面,它仍然具有https.我希望https浏览只有几页.如何将用户从Https重定向到http?是否可以使用RedirectToAction()方法?
到目前为止,我的https部署通常涉及使用https对整个站点的简单锁定,并在Web服务器上提供http-to-https重定向.
我现在计划在一个包含http和https页面的单个ASP.NET MVC站点(在云上).因此,该站点将有2个概念(非物理)区域,提供安全和非安全请求.
在配置方面,我已经为80和443设置了输入端口,并且该站点接受这两个请求.
有什么方法可以将协议转换为https,以进行属于安全区域的操作的任何调用吗?例如,动作过滤器可以执行的操作.
非常感谢.
编辑: 请注意,这样做的全部意图是避免在表单操作属性上使用绝对URL,因为可移植性问题以及用户将不会在浏览器上看到https://保证可视提示.
P
我需要在注册页面上使用https,在其他地方使用http.我在global.asax中编写了以下代码:
protected void Application_BeginRequest(object sender, EventArgs e)
{
var currentUrl = System.Web.HttpContext.Current.Request.Url;
if (currentUrl.AbsoluteUri.Contains("Registration"))
{
if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase))
{
//build the secure uri
var secureUrlBuilder = new UriBuilder(currentUrl);
secureUrlBuilder.Scheme = Uri.UriSchemeHttps;
//use the default port.
secureUrlBuilder.Port = string.IsNullOrEmpty(ConfigurationManager.AppSettings["HttpsPort"].ToString()) ? 443 : Convert.ToInt32(ConfigurationManager.AppSettings["HttpsPort"].ToString());
//redirect and end the response.
System.Web.HttpContext.Current.Response.Redirect(secureUrlBuilder.Uri.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这对于访问注册页面工作正常,但是当我访问其他页面时,该方案不会切换回http.
https ×5
asp.net-mvc ×2
annotations ×1
azure ×1
c# ×1
c#-4.0 ×1
cloud ×1
global-asax ×1
http ×1
ssl ×1