2010-01-19 27 views
6

Tôi đang sử dụng Request.IsSecureConnection để kiểm tra SSL và chuyển hướng khi thích hợp. Khi chạy trang web asp.net của tôi trên đám mây của Rackspace, máy chủ đang chạy phía sau cụm SSL, vì vậy IsSecureConnection sẽ luôn trả về false. Cũng vậy để kiểm tra xem url có chứa "https: //" hay không, luôn sai, kiểm tra cổng, v.v. Vì vậy, trang web bị kẹt trong vòng chuyển hướng lớn.Kiểm tra SSL khi được lưu trữ trong Rackspace (Mosso) Đám mây

Có cách nào khác để kiểm tra SSL và chuyển hướng khi thích hợp không? Bất cứ ai đã thực sự thực hiện điều này trên đám mây của Rackspace?

Public Class SecurityAwarePage 
    Inherits Page 

    Private _requireSSL As Boolean = False 

    Public Property RequireSSL() As Boolean 
     Get 
      Return _requireSSL 
     End Get 
     Set(ByVal value As Boolean) 
      _requireSSL = value 
     End Set 
    End Property 

    Private ReadOnly Property IsSecure() As Boolean 
     Get 
      Return Request.IsSecureConnection 
     End Get 
    End Property 

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs) 
     MyBase.OnInit(e) 

     PushSSL() 
    End Sub 

    Private Sub PushSSL() 
     Const SECURE As String = "https://" 
     Const UNSECURE As String = "http://" 

     If RequireSSL AndAlso Not IsSecure Then 
      Response.Redirect(Request.Url.ToString.Replace(UNSECURE, SECURE)) 
     ElseIf Not RequireSSL AndAlso IsSecure Then 
      Response.Redirect(Request.Url.ToString.Replace(SECURE, UNSECURE)) 
     End If 

    End Sub 

End Class 
+0

Không có gì liên quan đến câu hỏi, nhưng bạn có sự đánh giá cao về sử dụng const cho các chuỗi đơn giản như 'http' và 'https'. –

Trả lời

5

Mặc dù rất khó để kiểm tra xem SSL có tham gia vào vấn đề là buộc SSL hay không.

Từ RackspaceCloud Support knowledge base:

Bạn có thể URL trong web.config lại viết:

<configuration> 
<system.webServer> 
    <rewrite> 
    <rules> 
     <rule name="Redirect to HTTPS" stopProcessing="true"> 
     <match url=".*" /> 
     <conditions> 
      <add input="{HTTP_CLUSTER_HTTPS}" pattern="^on$" negate="true" /> 
      <add input="{HTTP_CLUSTER-HTTPS}" pattern=".+" negate="true" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}{SCRIPT_NAME}" redirectType="SeeOther" /> 
     </rule> 
    </rules> 
    </rewrite> 
</system.webServer> 
</configuration> 

Bạn có thể buộc SSL trong ASP.NET:

<%@ Page Language="C#" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<script runat="server"> 
    protected void Page_Load(object sender, System.EventArgs e) 
    { 
    if(Request.ServerVariables["HTTP_CLUSTER_HTTPS"] != "on") 
    { 
     if(Request.ServerVariables.Get("HTTP_CLUSTER-HTTPS") == null) 
     { 
     string xredir__, xqstr__; 

     xredir__ = "https://" + Request.ServerVariables["SERVER_NAME"]; 
     xredir__ += Request.ServerVariables["SCRIPT_NAME"]; 
     xqstr__ = Request.ServerVariables["QUERY_STRING"]; 

     if (xqstr__ != "") 
      xredir__ = xredir__ + "?" + xqstr__; 

     Response.Redirect(xredir__); 
     } 
    } 
    Response.Write("SSL Only"); 
    } 
</script> 

<html> 
<head id="Head1" runat="server"> 
    <title>SSL Only</title> 
</head> 
<body> 
</body> 
</html> 
+0

Cảm ơn, tôi đã tìm kiếm các tập tin trợ giúp và không tìm thấy bản thân mình. Nhìn lại, tôi cho rằng nó sẽ là thông minh để lặp lại bộ sưu tập ServerVariables và xem những gì đã có. –

+0

Tôi tò mò, là "HTTP_CLUSTER-HTTPS" một lỗi đánh máy? Bạn có một với hai dấu gạch dưới, và một với dấu gạch dưới và dấu gạch ngang. –

+0

Các liên kết [link] (http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/#Rule_action) viết lại các quy tắc thay thế dấu gạch ngang bằng dấu gạch dưới để nó có thể không quan trọng. Có vẻ như công việc chính xác như được hiển thị ở đây hoặc với cả hai dấu gạch dưới từ trải nghiệm trực tiếp của tôi. – philw

5

Tôi chạy vào này cùng một vấn đề với Rackspace Cloud và kết thúc việc giải quyết nó bằng cách thực hiện thủ công phương thức mở rộng Request.IsSecureConnection() và thay thế RequireHttpsAttribute của khung công tác bằng của riêng tôi. Hy vọng rằng người khác sẽ thấy điều này hữu ích là tốt.

/// <summary> 
/// Replaces framework-provided RequireHttpsAttribute to disable SSL requirement for local requests 
/// and properly enforce SSL requirement when used with Rackspace Cloud's load balancer 
/// </summary> 
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
public class RequireHttpsAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    public virtual void OnAuthorization(AuthorizationContext filterContext) { 
     if (filterContext == null) { 
      throw new ArgumentNullException("filterContext"); 
     } 

     if (filterContext.HttpContext.Request.IsLocal) 
      return; 

     if (!filterContext.HttpContext.Request.IsSecureConnection()) { 
      HandleNonHttpsRequest(filterContext); 
     } 
    } 

    protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext) { 
     // only redirect for GET requests, otherwise the browser might not propagate the verb and request 
     // body correctly. 

     if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { 
      throw new InvalidOperationException("The requested resource can only be accessed via SSL."); 
     } 

     // redirect to HTTPS version of page 
     string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl; 
     filterContext.Result = new RedirectResult(url); 
    } 

} 

public static class Extensions { 
    /// <summary> 
    /// Gets a value which indicates whether the HTTP connection uses secure sockets (HTTPS protocol). Works with Rackspace Cloud's load balancer 
    /// </summary> 
    /// <param name="request"></param> 
    /// <returns></returns> 
    public static bool IsSecureConnection(this HttpRequestBase request) { 
     const string rackspaceSslVar = "HTTP_CLUSTER_HTTPS"; 

     return (request.IsSecureConnection || (request.ServerVariables[rackspaceSslVar] != null || request.ServerVariables[rackspaceSslVar] == "on")); 
    } 

    /// <summary> 
    /// Gets a value which indicates whether the HTTP connection uses secure sockets (HTTPS protocol). Works with Rackspace Cloud's load balancer 
    /// </summary> 
    /// <param name="request"></param> 
    /// <returns></returns> 
    public static bool IsSecureConnection(this HttpRequest request) { 
     const string rackspaceSslVar = "HTTP_CLUSTER_HTTPS"; 

     return (request.IsSecureConnection || (request.ServerVariables[rackspaceSslVar] != null || request.ServerVariables[rackspaceSslVar] == "on")); 
    } 
} 
+0

vậy tại sao cần phải thực hiện thay thế cho lớp RequireHttpsAttribute? – Corgalore

+0

@Corgalore Vâng, bởi vì tôi không thể đơn giản thay thế HttpRequest.IsSecureConnection (một thuộc tính), đó là những gì được xây dựng trong RequireHttpsAttribute đang kiểm tra. Tôi đã thực hiện một * extension * trên HttpRequest gọi là IsSecureConnection() (một phương thức). Do đó, thay thế của tôi RequireHttpAttribute kiểm tra phần mở rộng của tôi để thay thế. –

Các vấn đề liên quan