【问题标题】:ASP.NET MVC Futures RequireSSL Attribute and Authorize Attribute TogetherASP.NET MVC Futures RequireSSL 属性和 Authorize 属性一起
【发布时间】:2009-07-15 18:25:13
【问题描述】:

是否有人在控制器上同时使用 Authorize 和 RequireSSL(来自 MVC 期货)属性?我创建了一个控制器,我必须强制执行用户必须登录并使用安全连接才能执行的规则。如果用户不在安全连接上,我希望应用程序重定向到 https,因此我在 RequireSSL 属性上使用 Redirect=true。代码看起来像(CheckPasswordExpired 是我自己开发的属性):

[Authorize]
[RequireSsl(Redirect = true)]
[CheckPasswordExpired(ActionName = "ChangePassword",
    ControllerName = "Account")]
[HandleError]
public class ActionsController : Controller
{
    ....
}

mysite.com/Actions/Index 是站点的默认路由,也是表单身份验证重定向到的默认页面。

当我浏览到http://mysite.com 时,我想要得到的是用户被重定向到一个安全连接,并且因为他们还没有经过身份验证,所以到登录页面。我得到的是 HTTP 400 错误(错误请求)。如果我导航到http://mysite.com/Account/Login,重定向有效,但我的帐户控制器和登录操作方法都没有 [Authorize] 属性。

任何人有使用这两个属性来实现我的目标的经验吗?

谢谢!

【问题讨论】:

    标签: asp.net-mvc asp.net-membership


    【解决方案1】:

    我正在成功地使用它们。你有默认动作的属性吗?

    public class HomeController : BaseController
    {
      [Authorize]
      [RequireSsl]
      public ActionResult Index ()
      {
      }
    }
    

    顺便说一句,我使用的是比期货稍有修改的版本,以便我可以全局禁用 SSL:

    [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public sealed class RequireSslAttribute : FilterAttribute, IAuthorizationFilter
    {
        public RequireSslAttribute ()
        {
            Redirect = true;
        }
    
        public bool Redirect { get; set; }
    
        public void OnAuthorization (AuthorizationContext filterContext)
        {
            Validate.IsNotNull (filterContext, "filterContext");
    
            if (!Enable)
            {
                return;
            }
    
            if (!filterContext.HttpContext.Request.IsSecureConnection)
            {
                // request is not SSL-protected, so throw or redirect
                if (Redirect)
                {
                    // form new URL
                    UriBuilder builder = new UriBuilder
                    {
                        Scheme = "https",
                        Host = filterContext.HttpContext.Request.Url.Host,
                        // use the RawUrl since it works with URL Rewriting
                        Path = filterContext.HttpContext.Request.RawUrl
                    };
                    filterContext.Result = new RedirectResult (builder.ToString ());
                }
                else
                {
                    throw new HttpException ((int)HttpStatusCode.Forbidden, "Access forbidden. The requested resource requires an SSL connection.");
                }
            }
        }
    
        public static bool Enable { get; set; }
    }
    

    【讨论】:

    • 感谢您的回复。我在控制器级别而不是操作级别附加了属性。顺便说一句,我喜欢你的启用增强功能。
    • 您如何使用启用?在我的开发环境中说我不希望它是 SSL?
    • Validate.IsNotNull(...) 做什么?我粘贴了代码,但是没有验证(我不想安装期货嘿嘿)
    • 启用我在应用程序启动期间根据 web.config 将其设置为 true/false。
    猜你喜欢
    • 2011-06-07
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    相关资源
    最近更新 更多