如何在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