0

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

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

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

4

1 回答 1

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

        ...
    }
}

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

于 2012-06-21T06:23:52.190 回答