【问题标题】:Where is the source for RequireHttpsAttribute?RequireHttpsAttribute 的来源在哪里?
【发布时间】:2011-02-28 22:36:27
【问题描述】:

ASP.NET MVC 包含强制 SSL 连接的 RequireHttpsAttribute 属性,但是在查看 codeplex 时,找不到它的源文件。我没有找对地方吗?

【问题讨论】:

    标签: asp.net-mvc codeplex


    【解决方案1】:

    我刚刚下载了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);
            }
    
        }
    }
    

    【讨论】:

    • 有趣的是,您无法通过浏览 codeplex 源来查看它。
    【解决方案2】:

    【讨论】:

    猜你喜欢
    • 2013-11-02
    • 2011-03-23
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多