2011-02-28 63 views

回答

8

我剛剛下載的源ASP.NET MVC 3 RTM,發現它在System.Web.Mvc項目:

namespace System.Web.Mvc { 
    using System; 
    using System.Diagnostics.CodeAnalysis; 
    using System.Web.Mvc.Resources; 

    [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "Unsealed because type contains virtual extensibility points.")] 
    [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.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(MvcResources.RequireHttpsAttribute_MustUseSsl); 
      } 

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

    } 
} 
+0

有趣的是,您無法通過瀏覽Codeplex源代碼來查看它。 – 2011-03-03 00:34:22