Asp.net mvc .NET-MVC-重写URL';s+;某些URL';什么是通过SSL?

Asp.net mvc .NET-MVC-重写URL';s+;某些URL';什么是通过SSL?,asp.net-mvc,ssl,Asp.net Mvc,Ssl,我有一个运行IIS 6.NET MVC的Web服务器,只有一个域名。该站点使用URL重写生成URL,如: domain.com/controller/action 我想强制一(1)个控制器使用SSL(其他控制器应在没有SSL的情况下工作)我应该如何实现这一点?您可以使用属性为需要SSL的控制器添加注释,或使其派生自标记有此属性的基本控制器。使用属性装饰需要SSL的控制器 尽管如此,如果您使用Cassini进行调试,您可能更喜欢对来自localhost的请求忽略这一点的自定义版本 [Attribu

我有一个运行IIS 6.NET MVC的Web服务器,只有一个域名。该站点使用URL重写生成URL,如:

domain.com/controller/action


我想强制一(1)个控制器使用
SSL
(其他控制器应在没有SSL的情况下工作)我应该如何实现这一点?

您可以使用属性为需要SSL的控制器添加注释,或使其派生自标记有此属性的基本控制器。

使用属性装饰需要SSL的控制器

尽管如此,如果您使用Cassini进行调试,您可能更喜欢对来自localhost的请求忽略这一点的自定义版本

[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:" ) );
    }
}

我已经使用了自定义的RequireSSL属性。但是,我的网站(IIS)尚未设置SSL。我只能通过为整个网站设置“目录安全性”来进行设置。或者这不会自动禁用正常的“http”请求吗?@ropstah——如果您在控制器/操作(来自本地连接以外的)上使用此属性,它将重定向到安全请求。另外,您应该注意,最初的请求实际上应该是GET,而不是POST。如果对受保护操作的第一个请求是POST,那么它将不起作用,因为重定向将是对同一url的获取(除了使用https)。我明白你的意思,没有问题。然而,我真正的问题可能是在IIS中设置SSL。“设置SSL”是否意味着“正常的”
http
(是不是
https
)请求仍适用于网站?(当然属性化控制器除外…)@ropstah-一般来说,是的,如果配置正确,您可以对同一网站发出SSL和非SSL请求。在MVC中,默认情况下站点不需要SSL,然后仅使用属性标记需要SSL的操作。请注意,在登录操作的GET/POST版本上都需要它。登录页面需要加密以防止中间人攻击。阅读时,作为安全措施,我们是否应该添加
过滤器。添加(新的RemoteRedirectToHttpsAttribute())
FilterConfig
中?
[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:" ) );
    }
}