我有一个运行 IIS 6、.NET MVC 的网络服务器,只有一个域名。该站点使用 URL 重写来生成如下 URL:
domain.com/controller/action
我想强制一 (1) 个 Controller 使用 SSL
(其他 Controller 应该在没有 SSL 的情况下工作)。 我应该如何实现?
最佳答案
用 RequireHttpsAttribute 修饰需要 SSL 的 Controller .
[RequireHttps]
public class SecureController : Controller
{
...
}
不过,如果您使用 Cassini 进行调试,您可能更喜欢忽略本地主机请求的自定义版本。
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false )]
public class RemoteRedirectToHttpsAttribute : RequireHttpsAttribute
{
public override void OnAuthorization( AuthorizationContext filterContext )
{
if (filterContext == null)
{
throw new ArgumentNullException( "filterContext" );
}
if (filterContext.HttpContext != null && (filterContext.HttpContext.Request.IsLocal || filterContext.HttpContext.Request.IsSecureConnection))
{
return;
}
filterContext.Result = new RedirectResult( filterContext.HttpContext.Request.Url.ToString().Replace( "http:", "https:" ) );
}
}
关于asp.net-mvc - .NET-MVC - 通过 SSL 重写 URL + 某些 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3072814/