这可能是一个新问题,但;

让我们说有一个ActionResult,我只想要授予访问权限以后小时。

让我们也说,我想要装饰我的ActionResult有一个定义属性。

这样的代码可能看起来像;

[AllowAccess(after="17:00:00", before="08:00:00")]
public ActionResult AfterHoursPage()
{
    //Do something not so interesting here;

    return View();
}

如何 到底 我会得到这工作吗?

我已经做了一些研究,在创建自定属性,但我认为我丢失的位如何使用它们。

请假设我知道几乎什么都没有关创建和使用他们虽然。

有帮助吗?

解决方案

尝试此(未测试的):

public class AllowAccessAttribute : AuthorizeAttribute
{
    public DateTime before;
    public DateTime after;

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null)
            throw new ArgumentNullException("httpContext");

        DateTime current = DateTime.Now;

        if (current < before | current > after)
            return false;

        return true;
    }
}

更多信息在这里: http://schotime.net/博客/ index.php的/ 2009/02/17 /定制授权与 - ASPNET-MVC /

其他提示

你在找什么。净视行动的过滤器。

你会需要延长ActionFilterAttribute类和实施OnActionExecuting方法在您的情况。

参见:http://www.asp.net/learn/mvc/tutorial-14-cs.aspx 对于一个体面介绍行动的过滤器。

也略有类似看到: ASP.NET 视-CustomeAuthorize过滤器行动使用一个外部网站登陆在用户

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top