我正在使用ASP.NET MVC 2并且有一个通过HTTPS保护的登录页面.为了确保用户始终通过SSL访问这些页面,我已将该属性添加[RequireHttps]到控制器.这完美地完成了这项工作.
当他们成功登录后,我想将它们重定向回HTTP版本.但是,没有[RequireHttp]属性,我正在努力探索如何实现这一目标.
增加的(潜在的)复杂性是生产中的网站托管在域的路由上,但是出于开发和测试的目的,它位于子目录/虚拟目录/应用程序中.
我是否过度思考这个问题并且有一个简单的解决方案让我盯着我?还是有点复杂?
我正在将一个asp.net应用程序转换为mvc3.假设我有一个需要https的登录页面,而且每个其他页面只需要http我如何将登录重定向到https并将所有其他页面保留在http?
任何建议都会很棒!
所以我找到了[RequiresHttps]属性,但是一旦你在https中你的那种卡在那里,所以试着能够对一个网址(和方案)进行操作我发现我最终不得不创建我自己的ExtendedController为不使用[RequireHttps]的操作恢复为http.
只是想知道我在做什么是好的还是有更好的方法?
public class ExtendedController : Controller
{
protected virtual void HandleHttpRequest(AuthorizationContext filterContext)
{
if (!string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Cannot post between https and http.");
}
string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
filterContext.Result = new RedirectResult(url);
}
protected override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
object[] attributes = filterContext.ActionDescriptor.GetCustomAttributes(true);
if (!attributes.Any(a => a is RequireHttpsAttribute))
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.HttpContext.Request.IsSecureConnection)
{
this.HandleHttpRequest(filterContext);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)