2009-07-13 24 views
16

मैं उपयोगकर्ताओं को HTTPS का उपयोग करके अपनी वेबसाइट देखने के लिए कैसे निर्धारित और मजबूर कर सकता हूं? मुझे पता है कि यह आईआईएस के माध्यम से किया जा सकता है, लेकिन यह जानना चाहता है कि यह प्रोग्रामेटिक तरीके से कैसे किया जाता है।सी # यह निर्धारित करने के लिए कि क्या HTTPS

+0

: http://www.jameskovacs.com/blog/HowToAutoRedirectToASSLsecuredSiteInIIS.aspx –

उत्तर

20

आप एक HttpModule इस प्रकार लिख सकते हैं:

/// <summary> 
/// Used to correct non-secure requests to secure ones. 
/// If the website backend requires of SSL use, the whole requests 
/// should be secure. 
/// </summary> 
public class SecurityModule : IHttpModule 
{ 
    public void Dispose() { } 

    public void Init(HttpApplication application) 
    { 
     application.BeginRequest += new EventHandler(application_BeginRequest); 
    } 

    protected void application_BeginRequest(object sender, EventArgs e) 
    { 
     HttpApplication application = ((HttpApplication)(sender)); 
     HttpRequest request = application.Request; 
     HttpResponse response = application.Response; 

     // if the secure connection is required for backend and the current 
     // request doesn't use SSL, redirecting the request to be secure 
     if ({use SSL} && !request.IsSecureConnection) 
     { 
      string absoluteUri = request.Url.AbsoluteUri; 
      response.Redirect(absoluteUri.Replace("http://", "https://"), true); 
     } 
    } 
} 

कहाँ {use SSL} एक कुछ शर्त है कि क्या SSL उपयोग करें या नहीं है।

संपादित: और, ज़ाहिर है, एक web.config करने के लिए एक मॉड्यूल परिभाषा जोड़ना भूल नहीं है:

<system.web> 
    <httpModules> 
     <!--Used to redirect all the unsecure connections to the secure ones if necessary--> 
     <add name="Security" type="{YourNamespace}.Handlers.SecurityModule, {YourAssembly}" /> 
     ... 
    </httpModules> 
</system.web> 
+0

अधिकांश वेब अनुप्रयोगों एक _global.asax_ पेज के रूप में एलेक्स ने संकेत दिया है कि यह भी एक ही कोड में शामिल कर सकते हैं। बस Application_BeginRequest हैंडलर – Rajiv

+0

प्रदान करें मेरी समस्या के साथ मुझे क्या मदद मिली।IsSecureConnection, इस मामले में अनुरोध को पूंजीकृत करने के लिए सुनिश्चित कर रहा है, जब तक कि आप एलेक्स जैसे एक var नामित अनुरोध नहीं बनाते। सिर्फ मन में रखने वाली कुछ बातें। –

+1

इस पर एक मामूली नोट है: आपको 'system.webServer' के तहत एकीकृत मोड कॉन्फ़िगरेशन को 'सत्यापन' टैग की 'validateIntegratedModeConfiguration' विशेषता' के साथ सेट किया गया है (देखें [यह उत्तर] (http://stackoverflow.com/ए/4210026) अधिक जानकारी के लिए)। इसके अतिरिक्त, यदि आप आईआईएस एक्सप्रेस या आईआईएस 6 का उपयोग करना चाहते हैं तो आपको [इस अन्य उत्तर] (http://stackoverflow.com/a/963975) –

2

इस आलेख में SSL के अंदर और बाहर चलने वाले अनुरोध शामिल हैं। कभी-कभी आप नहीं चाहते कि उपयोगकर्ता एसएसएल में एक पेज देख रहा हो क्योंकि यह उन पृष्ठों के लिए प्रो चक्र को जलाता है जिन्हें सुरक्षित करने की आवश्यकता नहीं है।

http://weblogs.asp.net/kwarren/archive/2005/07/08/418541.aspx

5

आप VB.NET से सी # में बदलने के लिए होगा, लेकिन यह है क्या मैं अपने साइटों में उपयोग करें:

Imports System.Web.HttpContext 

Public Shared Sub SetSSL(Optional ByVal bEnable As Boolean = False) 
    If bEnable Then 
    If Not Current.Request.IsSecureConnection Then 
     Dim strHTTPS As String = "https://www.mysite.com" 
     Current.Response.Clear() 
     Current.Response.Status = "301 Moved Permanently" 
     Current.Response.AddHeader("Location", strHTTPS & Current.Request.RawUrl) 
     Current.Response.End() 
    End If 
    Else 
    If Current.Request.IsSecureConnection Then 
     Dim strHTTP As String = "http://www.mysite.com" 
     Current.Response.Clear() 
     Current.Response.Status = "301 Moved Permanently" 
     Current.Response.AddHeader("Location", strHTTP & Current.Request.RawUrl) 
     Current.Response.End() 
    End If 
    End If 
End Sub 

यह अन्य तकनीकों में से कुछ की तुलना में अधिक कोड है, लेकिन इसके लिए एक कारण नहीं है। यह विधि केवल तभी रीडायरेक्ट की जाएगी जब यह उस मोड में नहीं है जिसमें यह होना चाहिए। और जब यह रीडायरेक्ट करता है, तो यह 301 (स्थायी) पुनर्निर्देशन करता है। लाभ यह है कि खोज इंजन 301 पुनर्निर्देशन का पालन करेंगे और इससे उन्हें एक ही पृष्ठ को दो बार अनुक्रमणित करने की संभावना को रोक दिया जाएगा (http और https मोड में)। आप इसे Response.Redirect (302 अस्थायी रीडायरेक्ट) के डिफ़ॉल्ट व्यवहार से तुलना कर सकते हैं, उदाहरण के लिए, Google, उसी तरह से व्यवहार नहीं करता है। वे अस्थायी रीडायरेक्ट के आधार पर अपनी अनुक्रमणिका नहीं बदलेंगे।

तो अगर आप एक पृष्ठ है कि आप एसएसएल-एन्क्रिप्टेड होना चाहता हूँ रहे हैं तो उसे इस तरह कहते हैं:

SetSSL (सच)

अन्यथा:

SetSSL (झूठी)

और यदि आपको वास्तव में वैश्विक रूप से लागू होने की आवश्यकता है, तो मैं आपके global.asax के Application_BeginRequest में SetSSL (True) को कॉल करूंगा। सावधान रहें कि एसएसएल चीजों को थोड़ा धीमा कर देगा। Http और https के बीच स्विच करते समय इसी कारण से मैं आमतौर पर बहुत चुनिंदा हूं। असल में, मैंने विकसित की गई दर्जनों साइटों में से केवल दो ही हैं जो पूरी साइट पर एसएसएल का उपयोग करते हैं।

1

IIR आप डोमेन के लिए अनुरोध (HttpContext.Current.Request) है जिसे आप जाँच कर सकते हैं क्या प्रोटोकॉल का इस्तेमाल किया जा रहा है (http, https, FTP, आदि)

10

थोड़ा कठिन कोडित लेकिन straighforward जाँच कर सकते हैं!

if (!HttpContext.Current.Request.IsSecureConnection) 
{ 
    Response.Redirect("https://www.foo.com/foo/"); 
} 
+1

+1 पर ध्यान देना चाहिए, एक http मॉड्यूल यहां ओवरकिल जैसा लगता है लेकिन मुझे यह पसंद है क्योंकि यह छोटा और प्यारा है, धन्यवाद। –

+1

सरल, लघु और काम करता है, – dvdmn

0

तुम भी ऊपर रीराइट नियम अपने web.config में system.webServer टैग के तहत निर्धारित कर सकते हैं। उदाहरण के लिए:

<rewrite> 
     <rules> 
     <rule name="Redirect to HTTPS" stopProcessing="true"> 
      <match url="(.*)" /> 
      <conditions> 
      <add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost(:\d+)?$" negate="true" ignoreCase="true" /> 
      <add input="{HTTP_HOST}" matchType="Pattern" pattern="^127\.0\.0\.1(:\d+)?$" negate="true" /> 
      <add input="{HTTPS}" pattern="off" /> 
      </conditions> 
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" /> 
     </rule> 
     </rules> 
    </rewrite> 
इस तरह
संबंधित मुद्दे