2010-01-19 13 views
6

में होस्ट किए जाने पर एसएसएल की जांच करें मैं एसएसएल की जांच करने और जहां उचित हो वहां पुनर्निर्देशित करने के लिए Request.IsSecureConnection का उपयोग कर रहा हूं। रैकस्पेस के क्लाउड पर मेरी एएसपीनेट वेबसाइट चलाते समय, सर्वर एक एसएसएल क्लस्टर के पीछे चल रहा है, इसलिए IsSecureConnection हमेशा झूठी वापसी करेगा। यह जांचने के लिए जाता है कि यूआरएल में "https: //" है, हमेशा झूठा है, बंदरगाह की जांच आदि है, इसलिए वेबसाइट बड़े रीडायरेक्ट लूप में फंस गई है।रैकस्पेस (मोसो) क्लाउड

एसएसएल की जांच करने और जहां उचित हो वहां रीडायरेक्ट करने का कोई और तरीका है? कोई भी जिसने वास्तव में इसे रैकस्पेस के क्लाउड पर किया है?

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

यह नहीं कि इस सवाल के साथ इसका कोई संबंध नहीं है, लेकिन आपको 'http' और 'https' जैसी सरल तारों के लिए कॉन्स्ट का उपयोग करने की मेरी प्रशंसा है। –

उत्तर

5

हालांकि यह जांचना मुश्किल है कि एसएसएल समस्या के आसपास एक रास्ता लगा रहा है या नहीं, एसएसएल को मजबूर करना है।

RackspaceCloud Support knowledge base से

:

आप web.config में यूआरएल को फिर से लिख सकते हैं:

<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> 

आप 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

धन्यवाद, मैंने सहायता फ़ाइलों की खोज की और मुझे वह नहीं मिला। वापस देखकर, मुझे लगता है कि सर्वरवारीबल्स संग्रह को लूप करने के लिए स्मार्ट होगा और देखें कि वहां क्या था। –

+0

मैं उत्सुक हूं, एक टाइपो "HTTP_CLUSTER-HTTPS" है? आपके पास दो अंडरस्कोर हैं, और अंडरस्कोर और डैश वाला एक है। –

+0

[लिंक] (http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/#Rule_action) नियमों को फिर से लिखना अंडरस्कोर के साथ डैश को प्रतिस्थापित करता है, इसलिए इससे कोई फर्क नहीं पड़ता। ऐसा लगता है कि यहां दिखाया गया है या मेरे प्रत्यक्ष अनुभव से दोनों अंडरस्कोर के साथ दिखाया गया है। – philw

5

मैं इस में भाग रैकस्पेस क्लाउड के साथ एक ही समस्या और मैन्युअल रूप से एक Request.IsSecureConnection() एक्सटेंशन विधि को कार्यान्वित करके और फ्रेमवर्क की RequHttpsAttribute को अपने आप से बदलकर इसे हल कर दिया। उम्मीद है कि किसी और को यह भी उपयोगी लगेगा।

/// <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

तो RequHttpsAttribute क्लास के प्रतिस्थापन को लागू करना क्यों आवश्यक है? – Corgalore

+0

@ कॉरगोर वेल, क्योंकि मैं बस HttpRequest.IsSecureConnection (एक प्रॉपर्टी) को प्रतिस्थापित नहीं कर सकता, जो कि अंतर्निहित RequHttpsAttribute की जांच कर रहा है। मैंने HttpRequest पर एक * एक्सटेंशन * बनाया है जिसे IsSecureConnection() (एक विधि) कहा जाता है। इस प्रकार, मेरे प्रतिस्थापन RequHttpAttribute इसके बजाय मेरे एक्सटेंशन की जांच करता है। –

संबंधित मुद्दे