有没有人成功地在一个控制器上同时使用Authorize和RequireSSL (来自MVC futures)属性?我已经创建了一个控制器,必须强制执行用户必须登录并使用安全连接才能执行的规则。如果用户不在安全连接上,我希望应用程序重定向到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属性。
有人有将这两个属性结合使用来实现我的目标的经验吗?
谢谢!
发布于 2009-07-15 18:37:32
我成功地使用了这两个工具。您的默认操作是否具有这些属性?
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; }
}https://stackoverflow.com/questions/1133079
复制相似问题