Fab*_*cio 1 asp.net-mvc asp.net-mvc-4
我想创建一个自定义的AuthorizeAttribute那
不知道怎么办.我的意思是,如何访问属性中的用户信息以进行检查?
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1029 次 |
| 最近记录: |