【问题标题】:Time based authentification and actions in asp.net mvcasp.net mvc 中基于时间的身份验证和操作
【发布时间】:2012-06-21 15:24:08
【问题描述】:

asp.net mvc 3 中是否有一种集成方式来允许基于一天中的时间进行身份验证和操作? 例如,18:00 时,属于特定角色的用户不被允许登录,或者如果他们已经通过身份验证,他们将被自动注销或无法执行任何操作。

我想在登录方法中我可以检查用户角色和时间,然后在每个操作中,我还会检查角色和时间并允许,但是有没有更简单的方法来完成这个?

更新: 我想没有更简单的方法来设置时间和用户/角色,所以我最终实现了答案(解决方案)。

【问题讨论】:

    标签: asp.net-mvc-3 forms-authentication


    【解决方案1】:

    您可以编写自定义 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
    
            ...
        }
    }
    

    现在剩下的就是用这个自定义属性来装饰你的控制器/动作。

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 2016-06-17
      • 2013-12-07
      • 2017-09-13
      相关资源
      最近更新 更多