1

我想创建一个自AuthorizeAttribute定义

  1. 检查用户登录。
    1. 如果记录:重定向到 place1。
    2. 否则:重定向到place2。
  2. 检查用户激活。
    1. 如果已登录 && 未激活:重定向到 place3

不知道该怎么做。我的意思是,如何访问属性内的用户信息以进行检查?

4

1 回答 1

5
public class MyAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAuthenticated)
        {
            // the user is authenticated => redirect to place1
            // you could get the current user from the 
            // filterContext.HttpContext.User property and query your provider
            // to verify if he is activated (whatever that means in your specific context)

            var routeValues = new RouteValueDictionary(new
            {
                contoller = "foo",
                action = "bar",
            });
            filterContext.Result = new RedirectToRouteResult(routeValues);
        }
        else
        {
            // the user is not authenticated => redirect to place2
            var routeValues = new RouteValueDictionary(new
            {
                contoller = "bazingaS",
                action = "theBaz",
            });
            filterContext.Result = new RedirectToRouteResult(routeValues);
        }
    }
}
于 2013-06-14T20:20:32.643 回答