如何在MVC中创建自定义属性以扩展现有的Authorize属性?asp.net mvc添加到AUTHORIZE属性
12
A
回答
17
从AuthorizeAttribute派生你的类。重写OnAuthorization方法。添加并设置一个CacheValidationHandler。
public void CacheValidationHandler(HttpContext context,
object data,
ref HttpValidationStatus validationStatus)
{
validationStatus = OnCacheAuthorization(new HttpContextWrapper(context));
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (AuthorizeCore(filterContext.HttpContext))
{
... your custom code ...
SetCachePolicy(filterContext);
}
else if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
// auth failed, redirect to login page
filterContext.Result = new HttpUnauthorizedResult();
}
else
{
... handle a different case than not authenticated
}
}
protected void SetCachePolicy(AuthorizationContext filterContext)
{
// ** IMPORTANT **
// Since we're performing authorization at the action level, the authorization code runs
// after the output caching module. In the worst case this could allow an authorized user
// to cause the page to be cached, then an unauthorized user would later be served the
// cached page. We work around this by telling proxies not to cache the sensitive page,
// then we hook our custom authorization code into the caching mechanism so that we have
// the final say on whether a page should be served from the cache.
HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
cachePolicy.SetProxyMaxAge(new TimeSpan(0));
cachePolicy.AddValidationCallback(CacheValidationHandler, null /* data */);
}
3
public class CoolAuthorizeAttribute : AuthorizeAttribute
{
}
10
您不需要扩展此属性,web.config就足够了。请阅读有关forms Element for authentication。关注defaultUrl。这是你需要的东西。
<system.web>
<authentication mode="Forms">
<forms defaultUrl="YourUrlGoesHere"/>
</authentication>
</system.web>
0
我建议,如果你只是想延长现行AuthorizeAttribute,并添加最重要的是你自己的授权,而不是覆盖OnAuthorization只是覆盖AuthorizeCore并添加MyCustomAuthorizationHolds条件吧。
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
// This method must be thread-safe since it is called by the thread-safe OnCacheAuthorization() method.
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (base.AuthorizeCore(httpContext) && MyCustomAuthorizationHolds)
return true;
return false;
}
}
相关问题
- 1. 将属性添加到ASP.NET MVC 2 ViewUserControl
- 2. ASP.NET MVC添加HiddenInput属性的DLL
- 3. asp.net mvc dataannotions附加属性
- 4. 如何将id属性添加到asp.net mvc中的Html.BeginForm()?
- 5. ASP.NET MVC的形式编辑和添加到集合属性
- 6. asp.Net MVC将属性添加到用户类
- 7. 如何将html5数据属性添加到asp.net mvc中的Html.TextBox?
- 8. ASP.NET MVC:如何将嵌套属性添加到Modelstate?
- 9. ASP.NET MVC属性
- 10. 将'Current'属性添加到ASP.NET控件
- 11. asp.net:添加属性头次
- 12. ASP.NET MVC - DisplayFormat属性
- 13. 添加分页到ASP.NET MVC
- 14. ASP.NET MVC - 停止Html.TextBoxFor向Name属性添加一个点
- 15. 动态添加/在ASP.NET MVC除去项阵列属性4次
- 16. 如何在ASP.NET MVC中添加ID HTML属性瓦特/ VB.NET
- 17. ASP.NET MVC 2:在ActionFilter中添加绑定(前缀)属性
- 18. 在Route属性ASP.NET MVC的URL中添加自定义单词
- 19. ASP.NET MVC添加属性“数据信息”,以Html.TextBoxFor
- 20. 向应用程序用户添加其他属性ASP.NET MVC
- 21. 添加列表<T>属性ASP.net MembershipUser对象MVC 4
- 22. 如何在asp.net mvc中添加布尔属性?
- 23. 添加自定义属性页面指令在asp.net-MVC
- 24. 如何在asp.net中为DropDownList添加多个属性mvc
- 25. asp.net mvc添加不带属性的模型元数据
- 26. 在用户登录后向ASP.NET MVC Identity添加定制属性
- 27. 将属性添加到属性
- 28. 添加属性到属性窗口
- 29. Magento将Sub属性添加到属性?
- 30. 向Spring MVC Session添加新属性
请添加更多细节,你想要延伸到什么程度? – 2009-02-16 18:30:27
现在我只是想能够重定向到正确的页面,而不是默认的主页。 – zsharp 2009-02-16 18:33:24
你可以更新你的问题,这样每个人都可以知道你需要什么。 – 2009-02-16 19:25:24