2012-02-20 4 views
7

Khi bạn đặt [RequireHttps] trên một hành động và người sử dụng công tắc từ HTTP thành HTTPS, tất cả các liên kết tiếp theo sẽ ở lại HTTPS ...ASP.NET MVC [RequireHttps] - trở về http

Có cách nào để chuyển về HTTP?

+0

Điều này có thể được thực hiện với các bộ lọc. Hãy thử tìm kiếm SO, có rất nhiều câu hỏi gần như chính xác giống như của bạn. –

Trả lời

6

Về mặt kỹ thuật, bạn có thể làm điều đó

Bạn có thể look at the source của RequireHttpsAttribute và đảo ngược.

Trong thực tế, có lẽ bạn không nên

Nếu phiên giao dịch vẫn còn sống, it is generally inadvisable to return to HTTP. Đây có thể là số foundation for a variety of attacks, ví dụ: session hijacking.

+0

Cảm ơn tất cả các liên kết - Tôi thường đồng ý với bạn, nhưng tôi có trang "liên hệ với chúng tôi" mà tôi muốn thực thi SSL - phần còn lại của trang web là thông tin. – zam6ak

+0

@ zam6ak Không sao cả. Bạn sẽ được OK nếu phần còn lại của trang web chỉ là thông tin, nhưng bạn hy vọng sẽ đạt được gì khi trở lại HTTP? –

+0

Tôi đọc ở đâu đó mà chuyển đổi các chương trình "đau" SEO. Thật không may, tôi không có liên kết bài viết nữa, và tôi không chắc chắn 100% nếu điều đó đúng ... – zam6ak

1

Dưới đây là thuộc tính 'ExitHttpsIfNotRequired' Tôi sử dụng:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] 
public class RetainHttpsAttribute : Attribute 
{ 
} 

public class ExitHttpsIfNotRequiredAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    public void OnAuthorization(AuthorizationContext filterContext) 
    { 
     // Abort if it's not a secure connection 
     if (!filterContext.HttpContext.Request.IsSecureConnection) return; 

     if (filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == "sdsd") return; 

     // Abort if it's a child controller 
     if (filterContext.IsChildAction) return; 

     // Abort if a [RequireHttps] attribute is applied to controller or action 
     if (filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return; 
     if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return; 

     // Abort if a [RetainHttps] attribute is applied to controller or action 
     if (filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(RetainHttpsAttribute), true).Length > 0) return; 
     if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(RetainHttpsAttribute), true).Length > 0) return; 

     // Abort if it's not a GET request - we don't want to be redirecting on a form post 
     if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) return; 

     // Abort if the error controller is being called - we may wish to display the error within a https page 
     if (filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == "Error") return; 

     // No problems - redirect to HTTP 
     string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl; 
     filterContext.Result = new RedirectResult(url); 
    } 
}