【问题标题】:Prevent to go another page until form is submitted在提交表单之前阻止转到另一页
【发布时间】:2014-08-26 08:44:25
【问题描述】:

在我的项目中,我有一个功能,每当创建新用户并登录帐户时,他必须先更改密码。

所以如果用户是新用户,我会在登录后首先显示 ChangePassowrd 屏幕。菜单只存在 ChangePassowrd 和 LogOut 选项。

现在如果假设他输入了一些只有在用户更改密码后才能访问的网站 url,这是不允许的。有谁知道我可以如何实现它?我知道widnow.unload,但如果直接粘贴url,它就不起作用。 在服务器端,我有一个 BaseController 在任何控制器之前调用(但在控制器的构造函数调用之后)进行初始化。我试图改变 RouteData.Values 但它没有用。它抛出错误 404 未找到。代码如下:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
  if (requestContext != null)
  {
    base.Initialize(requestContext);
    if (Convert.ToString(requestContext.HttpContext.Session["UserType"]) == "3")
    {
      requestContext.RouteData.Values["controller"] = "Login";
      requestContext.RouteData.Values["action"] = "ChangePassword";
    }
  }
}

【问题讨论】:

  • 我认为这在客户端是不可能的。用户只需通过 url 打开新标签,因此您必须进行服务器端检查。
  • 你不能只使用控制器代码吗?剩下的文件呢?如果你只想在前端做,那是不可能的。您必须使用服务器端。
  • @mo.esmp 好的。但是如何在服务器端实现呢?

标签: c# javascript jquery asp.net-mvc asp.net-mvc-5


【解决方案1】:

您可以在用户模型中插入一个新属性,这意味着扩展 asp.net 身份模型并使这个新属性类似于 public bool passwordChanged { get;放;} 选中此属性,除非更改密码,否则不应允许访问。

【讨论】:

    【解决方案2】:

    您可以通过这种方式实现这一目标。

    AppStart/FilterConfig.cs文件中添加

    filters.Add(new ChangePasswordRequiredActionFilter());
    

    现在,您必须为该过滤器添加实现。您可以在global.asax 文件中或任何您想要的地方执行此操作:

    public class ChangePasswordRequiredActionFilter: IActionFilter
    {
        #region Implementation of IActionFilter
    
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string currentUrl = HttpContext.Current.Request.Url.AbsolutePath;
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                //Now you have to put your custom logic here, this is example:
    
                User user = ourService.GetUser(blablabla);
                if (user != null && !user.ChangePassword)
                {
    
                    if (currentUrl != "/Account/ChangePassword" && currentUrl != "/Account/LogOff")
                    {
                        filterContext.Result = new RedirectResult("/Account/ChangePassword");
                    }
    
                }
            }
        }
    
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }
    
        #endregion
    }
    

    此解决方案可确保您检查每个用户对服务器的请求的条件。

    【讨论】:

    • 我无法在这里进行会话。
    • 什么会话?您在这里不使用会话。您只需对Request 系统变量进行操作。
    【解决方案3】:

    在用户登录并更改密码之前,只需将用户状态设置为“未登录”即可。

    如果这个问题是针对某些技术的,那么从提问的方式来看并不明显。

    【讨论】:

      【解决方案4】:

      对于你想要的这个功能,你必须在你的 webapp 中创建一个自定义过滤器,如下所示......

       public class ChangePasswordAttribute : ActionFilterAttribute
       {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
                  //Check user has changed password or not 
                   if(--yes--){ nothing to do }
                   else{ GoToChangePasswordPage(filterContext); }
                   return;
          }
      
          private static void GoToChangePasswordPage(ActionExecutingContext filterContext)
          {
              filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary 
                  { 
                      { "controller", "Login" }, 
                      { "action", "ChangePassword" } 
                  });
          }
       }
      

      有关如何在 asp.net mvc 中构建自定义过滤器的更多帮助,请访问.. 1.)http://www.dotnetcurry.com/showarticle.aspx?ID=976

      2.)http://sampathloku.blogspot.in/2013/03/how-to-create-custom-action-filter-for.html

      【讨论】:

        【解决方案5】:

        最后,我在 BaseController 中使用以下代码解决了它:我认为这是更简单的版本。

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
          if (filterContext != null)
          {
            if (Session["UserType"] != null && Convert.ToString(Session["UserType"]) == "3") // New User
            {
              filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = "ChangePassword", controller = "Login" }));
            }
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-07
          • 2022-08-17
          • 1970-01-01
          相关资源
          最近更新 更多