2008-12-08 50 views
4

作爲未經證實的性感技術的饕I,我在我的Web Forms應用程序中採用了System.Web.Routing來管理導航等。此外,我希望將基於角色的安全性從web.config移動到路由定義本身,因此我可以說「此路由僅適用於角色x,y」。在IRouteHandler.GetHttpHandler()我可以重定向嗎?

所以我有一個實現IRouteHandler的類,在它試圖加載一個特定的頁面之前,它檢查用戶是否在允許的角色集合中。我的問題是,如果他們不是,我該如何重定向到該處理程序中的登錄頁面?我知道可以在這個實例中加載登錄頁面,但我更喜歡乾淨的重定向與「returnto」頁面和全部。

public IHttpHandler GetHttpHandler(RequestContext requestContext) { 

if (AllowedRoles != null) 
{ 
    bool allowed = false; 

    for (int i = 0; i < AllowedRoles.Length; i++) 
    { 
     if (requestContext.HttpContext.User.IsInRole(AllowedRoles[i])) 
     { 
      allowed = true; 
      break; 
     } 
    } 

    if (!allowed) 
    { 
     ??? 
    } 
} 

回答

4

可以從GetHttpHandler進行重定向。只需使用:

requestContext.HttpContext.Response.Redirect("login.aspx"); 
相關問題