RedirecToAction总是将我发送回默认页面

时间:2016-09-23 16:31:55

标签: c# asp.net-mvc global-asax

我有一个项目,我做了一些重大改变。当然这之前有用,但从那时起我就破坏了一些东西。我做了一个比较,我无法找到我打破的原因。

这是我所处的方法:

public ActionResult Logon2(LogOnModel model)
{
  var control = Logging.StartLog();
  control.ClassName = System.Reflection.MethodBase.GetCurrentMethod().Name;

  try
  {
    Logging.WriteLog(control, "Starting Logon2");
    Logging.WriteLog(control, "corpid: " + CurrentCustomerSession.Current.CorporationId);
    Logging.WriteLog(control, "model.UserName: " + model.UserName);
    Logging.WriteLog(control, "model.Password: " + model.Password);

    Logging.WriteLog(control, string.Format("ModelState.IsValid: {0}", ModelState.IsValid));
    if (ModelState.IsValid)
    {
      if (Membership.ValidateUser(model.UserName, model.Password))
      {
        Logging.WriteLog(control, "Validated User");
        Logging.WriteLog(control, string.Format("model.UserName: {0}", model.UserName));

        Logging.WriteLog(control, "Start GetCustomerForAccount(model.UserName)");
        var svc = new SubService();
        var customer = svc.GetCustomerForAccount(model.UserName, CurrentCustomerSession.Current.CorporationId);

        // ********** NO ACCOUNTS FOUND ************
        // No customers assigned to this login
        // take them to the add account screen
        if (customer == null)
        {
          // no accounts tied to this logon
          Logging.WriteLog(control, "No accounts found for this login.");
          Logging.WriteLog(control, "RedirectToAction(AddCustomer, Customer)");
          // we need to make sure the branch account number is blank from any prior visits
          CurrentCustomerSession.Current.AccountGuid = Guid.Empty;
          CurrentCustomerSession.Current.Branch = string.Empty;
          CurrentCustomerSession.Current.AccountNumber = string.Empty;
          return RedirectToAction("AddCustomer", "Customer");
        }

        Logging.WriteLog(control, string.Format("customer.AccountId: {0}", customer.AccountId));
        Logging.WriteLog(control, string.Format("customer.Branch: {0}", customer.Branch));
        Logging.WriteLog(control, string.Format("customer.AccountNumber: {0}", customer.AccountNumber));

        CurrentCustomerSession.Current.AccountGuid = Guid.Parse(customer.AccountId);
        CurrentCustomerSession.Current.Branch = customer.Branch;
        CurrentCustomerSession.Current.AccountNumber = customer.AccountNumber;
        CurrentCustomerSession.Current.Name = customer.Name;
        CurrentCustomerSession.Current.PhoneNumber = string.Format("({0}) {1}", customer.AreaCode, customer.PhoneNumber);

        model.AccountId = customer.AccountId;

        // only 1 account tied to this logon
        return RedirectToAction("AccountScreen", "Customer");
      }
      else
      {
        var user = Membership.GetUser(model.UserName);
        if (user != null && user.IsLockedOut)
        {
          const string lockedmsg = "This account has been locked due to too many login attempts. Please reset your password to unlock this account.";
          Logging.WriteLog(control, lockedmsg);
          ModelState.AddModelError(string.Empty, lockedmsg);
        }

        if (user != null && !user.IsApproved)
        {
          const string lockedmsg =
            "This account has been registered but the email that was sent was never authenticated. Please check your email account for this email.";
          Logging.WriteLog(control, lockedmsg);
          ModelState.AddModelError(string.Empty, lockedmsg);
        }
        else
        {
          const string msg = "Invalid username or password.";
          Logging.WriteLog(control, msg);
          ModelState.AddModelError(string.Empty, msg);
        }
      }
    }

    // If we got this far, something failed, redisplay form
    return View("Logon");
  }
  catch (Exception ex)
  {
    Logging.WriteLog(control, string.Format("exception: {0}", ex.Message));
    Logging.WriteException(control, ex);
    return View("Logon");
  }
  finally
  {
    Logging.WriteLog(control, "End Logon2");
    Logging.FlushLog(control);
  }
}

两个重定向行为都将我发送到默认页面。我对这些行进行了调试,但是他们正在联系我们,但我总是以我的默认页面结束。

这是在我的Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute("UnhandledExceptions", "Error", new {controller = "Error", action = "Index"});
  routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Account", action = "Logon", id = UrlParameter.Optional});
}

protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();    
  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);      
}

这门课没有改变。我没有收到任何我能看到的错误消息。我介入并以我的登录方式结束了。

我不确定发生了什么。

有人有任何建议吗?

编辑2

我的登录方法,如果有帮助:

public ActionResult LogOn(string id)
{
  var control = Logging.StartLog();
  control.ClassName = System.Reflection.MethodBase.GetCurrentMethod().Name;

  try
  {
    Logging.WriteLog(control, "Starting LogOn");
    Logging.WriteLog(control, "id: " + id);

    CurrentCustomerSession.Current.SessionId = Session.SessionID;

    // set to expired if the session has timed out
    if (id == "expired")
    {
      const string msg = "Your session has expired. Please log back in if you wish to continue.";
      Logging.WriteLog(control, "Message given: " + msg);

      CurrentCustomerSession.Current.LoginMessage = msg;
      id = string.Empty;
    }
    else
    {
      CurrentCustomerSession.Current.LoginMessage = string.Empty;
    }

    // we are no longer relying on cookies since a customer could be
    // coming from one company to another such as Wells to Western States 
    Guid nid;
    if (!Guid.TryParse(id, out nid))
    {
      if (CurrentCustomerSession.Current.TokenId == Guid.Empty)
      {
        // if it failed in any of the checks then we give an invalid token page
        Logging.WriteLog(control, string.Format("TokenId check failed for: [{0}]", id));
        return View("InvalidToken");
      }            
    }

    // we've made it this far that means we have a tokenid
    if (nid != Guid.Empty)
    {
      CurrentCustomerSession.Current.TokenId = nid;
    }

    RecordVisitorInfo.Record(Request, nid);

    if (User.Identity.IsAuthenticated)
    {
        Logging.WriteLog(control, "Starting LogOff");
        FormsAuthentication.SignOut();
        Logging.WriteLog(control, "Signed Out");
    }
    return View();
  }
  catch (Exception ex)
  {
    Logging.WriteException(control, ex);
    Logging.WriteLog(control, string.Format("exception: {0}", ex.Message));
    return View("Error");
  }
  finally
  {
    Logging.WriteLog(control, "End Logon");
    Logging.FlushLog(control);
  }
}

0 个答案:

没有答案