2012-06-21 29 views
0

在asp.net mvc 3中是否有一種集成方式,可以根據一天的時間進行認證和操作? 例如,如果是18:00點,屬於特定角色的用戶將不被允許登錄,或者如果他們已經通過身份驗證,他們將被自動註銷或無法執行操作。在asp.net中進行基於時間的認證和操作mvc

我想在登錄方法中,我可以檢查用戶角色和每天的時間,然後在每個動作上,我還會檢查角色和時間以及許可,但有沒有更簡單的方法來完成此操作?

更新: 我想沒有更簡單的方法來設置時間和用戶/角色,所以我最終實現了答案(解決方案)。

回答

0

你可以寫一個自定義的Authorize屬性和覆蓋AuthorizeCore方法,其中,你會進行必要的檢查:

public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var authorized = base.AuthorizeCore(httpContext); 
     if (!authorized) 
     { 
      return false; 
     } 

     // At this stage standard authorization passed => 
     // you could now check the user roles in the database 
     // and the time of the day and return true or false from here 

     ... 
    } 
} 

現在所有剩下的就是這個自定義屬性裝飾你的控制器/行動。