c# - 全局应用操作过滤器在 MVC 中给出重定向循环异常

标签 c# asp.net-mvc razor global-asax asp.net-mvc-filters

问题陈述:

如果 session 过期或用户尝试访问任何 View 而不使用 razor 登录 MVC 4,我将尝试将用户重定向到登录页面。

  • 如果我使用,而不是在 filter.config 中全局应用 Action 过滤器 每个操作方法的过滤器属性都可以正常工作。
  • 我不想将此操作过滤器应用于每个操作 方法。

我想全局应用它。如何通过全局应用操作过滤器来避免重定向循环??如何实现??

登录 Controller :

//Get Method
public ActionResult Index(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}

//Post Method
[HttpPost]
public ActionResult Index(LoginModel loginModel, string returnUrl)
{
    if (ModelState.IsValid)
    {
        objLoginDC.LoginID = loginModel.LoginID;
        objLoginDC.Password = loginModel.Password;
        if (objSvcMasterConfig.IsValid(objLoginDC))
        {
            var varLoginTenantUserDetails = objSvcMasterConfig.GetLoginUserDetails(objLoginDC);
            Session["User"] = varLoginUserDetails[0];
            FormsAuthentication.SetAuthCookie(objLoginDC.LoginID, objLoginDC.RememberMe);

            return RedirectToLogin(returnUrl);
        }
        else
        {
            ModelState.AddModelError("", "The Log In ID or Password provided is incorrect.");
        }
    }

    return View(loginModel);
}

过滤器( Action 过滤器):

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class CheckUserSessionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpSessionStateBase session = filterContext.HttpContext.Session;
        var user = session["User"];

        if (((user == null) && (!session.IsNewSession)) || (session.IsNewSession))
        {
            session.RemoveAll();
            session.Clear();
            session.Abandon();

            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Login" }, { "action", "Index" } });
        }
        base.OnActionExecuting(filterContext);
    }
}

过滤器配置.cs:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new Filters.CheckUserSessionAttribute());
    }
}

最佳答案

当 Controller 为登录且操作为索引时,您可以尝试不执行过滤器逻辑:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class CheckUserSessionAttribute : ActionFilterAttribute
{

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //do not execute the filter logic for Login/Index
        if (filterContext.RouteData.GetRequiredString("controller").Equals("LogIn", StringComparison.CurrentCultureIgnoreCase)
            && filterContext.RouteData.GetRequiredString("action").Equals("Index", StringComparison.CurrentCultureIgnoreCase)){
            return;
        }

        HttpSessionStateBase session = filterContext.HttpContext.Session;
        var user = session["User"];

        if (((user == null) && (!session.IsNewSession)) || (session.IsNewSession))
        {
            session.RemoveAll();
            session.Clear();
            session.Abandon();

            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Login" }, { "action", "Index" } });
        }
        base.OnActionExecuting(filterContext);
    }
}

关于c# - 全局应用操作过滤器在 MVC 中给出重定向循环异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22935322/

相关文章:

c# - 如何在 Lucene.NET 中进行部分词搜索?

java - 使用 C# 调用 Yellowfin 商业智能服务

c# - 验证唯一性

c# - 使用模型的 ASP.NET MVC 重定向

javascript - MVC Razor 表中的复选框检查选择为 false

c# - 将电子邮件作为主键是个坏主意吗?

c# - 无法引用 Microsoft.SharePoint.Client.dll

c# - ASP.NET MVC 使用 View 中的值填充 DropDownList

asp.net-mvc-3 - 在 ASP MVC 3 中获取单选按钮值列表

c# - 是否可以将附加数据从属性传递到错误 View ?