2009-10-15 12 views
12

मैं एएसपी.नेट एमवीसी 2 पूर्वावलोकन 2 फ्यूचर्स आवश्यकता एचटीपीएस विशेषता का उपयोग कैसे करूं?एएसपी.नेट एमवीसी आवश्यकता हैट्स

मैं असुरक्षित HTTP अनुरोधों को एक क्रिया विधि में भेजने से रोकना चाहता हूं। मैं स्वचालित रूप से HTTPS पर रीडायरेक्ट करना चाहता हूं।

MSDN:

मैं कैसे इस सुविधा का उपयोग करते हैं?

उत्तर

11

मेरा अनुमान है:

[RequireHttps] //apply to all actions in controller 
public class SomeController 
{ 
    //... or ... 
    [RequireHttps] //apply to this action only 
    public ActionResult SomeAction() 
    { 
    } 

} 
+1

ऐसा लगता है कि HTTP अनुरोधों को रोकने के लिए प्रतीत होता है, लेकिन यह HTTPS पर रीडायरेक्ट नहीं करता है। –

+0

नहीं। यह विजुअल स्टूडियो के एएसपी.NET विकास सर्वर के साथ बस एक समस्या हो सकती है। http://stackoverflow.com/questions/60113/ –

+5

एएसपी.नेट एमवीसी आवश्यकता केवल उत्पादन में: http://stackoverflow.com/questions/1639707/asp-net-mvc-requirehttps-in-production- केवल –

15

मुझे लगता है कि आपको लगता है कि के लिए अपनी खुद की ActionFilterAttribute रोल करने की जरूरत जा रहे हैं।

public class RedirectHttps : ActionFilterAttribute { 
    public override void OnActionExecuting(ActionExecutingContext filterContext) { 
     if (!filterContext.HttpContext.Request.IsSecureConnection) { 
      filterContext.Result = 
       new RedirectResult(filterContext.HttpContext.Request.Url. 
        ToString().Replace("http:", "https:")); 
      filterContext.Result.ExecuteResult(filterContext); 
     } 
     base.OnActionExecuting(filterContext); 
    } 
} 

फिर अपने नियंत्रक में:

public class HomeController : Controller { 

    [RedirectHttps] 
    public ActionResult SecuredAction() { 
     return View(); 
    } 
} 

साथ ही आप this भी पढ़ सकते हैं।

+0

पोस्ट विधि के लिए लक्षित एक क्रिया में इसे जोड़ने पर सावधान रहें। – Carl

+1

@ करल क्यों? क्योंकि पोस्ट डेटा खो गया है? यदि आप यह सुनिश्चित करना चाहते हैं कि गैर-https पर संवेदनशील डेटा पोस्ट नहीं किया जा रहा है, तो आपको उस डेटा को संसाधित नहीं करना चाहिए। – eglasius

+1

@ çağdaş आप इस योजना को बदलने के लिए इस विधि का उपयोग करना चाह सकते हैं - स्ट्रिंग को प्रतिस्थापित करने से सुरक्षित होना चाहिए: http://stackoverflow.com/questions/17968426/changing-the-checheme-of-system-uri –

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