我有一个 ProfileRequiredActionFilter 的实现。它检查用户是否正确填写了个人资料 - 如果没有,它会在 RegisterSecondStep 上重定向用户。
问题是这种永久重定向用户循环..
public class ProfileRequiredActionFilter : IActionFilter
{
#region Implementation of IActionFilter
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
User user = UserTools.getUser(HttpContext.Current.User.Identity.Name);
if (user.NickName == null || user.Firstname == null || user.Surname == null || user.StateId == 0)
{
filterContext.Result = new RedirectResult("/Account/RegisterSecondStep");
}
}
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
}
#endregion
}
我在我的代码中添加了这部分 - 现在用户重定向一次到 RegisterSecondStep,并且不允许做任何事情。一般 - 不错。但是如何让用户退出呢?这个按钮也被阻止了,如果我点击“注销”,我会被重定向到RegisterSecondStep
......
public class ProfileRequiredActionFilter : IActionFilter
{
#region Implementation of IActionFilter
public void OnActionExecuting(ActionExecutingContext filterContext)
{
string text = HttpContext.Current.Request.Url.AbsolutePath;
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
User user = UserTools.getUser(HttpContext.Current.User.Identity.Name);
if (user.NickName == null || user.Firstname == null || user.Surname == null || user.StateId == 0)
{
if (text != "/Account/RegisterSecondStep")
{
filterContext.Result = new RedirectResult("/Account/RegisterSecondStep");
}
}
}
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
}
#endregion
}
这就是我运行此过滤器的方式
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new ProfileRequiredActionFilter());
}