Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

RedirectToAction with Parameter as userId (string) not working

I am having an issue with Redirect action. When a user logs in they will be redirected to the Dashboard. The dashboard takes a Parameter of userId.

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
        switch (result)
        {
            case SignInStatus.Success:
                ApplicationUser user = await UserManager.FindAsync(model.Email, model.Password);
                // Redirect to User landing page on SignIn, according to Role
                var userId = User.Identity.GetUserId();
                if ((UserManager.IsInRole(user.Id, "CompanyAdmin")))
                {
                    return RedirectToAction("Index", "Customer", new { UserId = userId }, null);
                }
                if ((UserManager.IsInRole(user.Id, "Administrator")))
                {
                    return RedirectToAction("Index", "Admin");
                }
                return View(model);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }

Note: The line above that is giving me grief is this:

return RedirectToAction("Index", "Customer", new { UserId = userId }, null);

I have also tried:

return RedirectToAction("Index", "Customer", new { UserId = userId });

It also does not seem to work.

Visual Studio pic

[EDIT]

    [Authorize(Roles = "CompanyAdmin")]
    public ActionResult Index(string UserId)
    {
        if (UserId == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        //Trying to get a view model for customer from the received UserId.
        CompanyOverview modelInstance = CompanyOverview.GetCompanyByUser(UserId, db);
        if (modelInstance == null)
        {
            return HttpNotFound();
        }
        return View(modelInstance);
    }

[EDIT] - New Index Action that is working. It appears for some reason the userId is getting stripped off and the index needs the user id to display the dashboard. So i have created a kind of circle around the issue. In the notes i indicated that there was a redirect to a Welcome page and a button on this page to go to setting up your profile. You have to be in the Role of CompanyAdmin to access this page. So the system automatically redirects the person to login. then with the current redirect on Login, it tries to send them to the dashboard. This is not complete until they create the profile, which makes the model null. So i have done 2 redirects here that seem to be working. Again if someone reaches that page and is not a user they will get redirected to either login or create an account.

        [Authorize(Roles = "CompanyAdmin")]
    public ActionResult Index(string UserId)
    {
        var userId = User.Identity.GetUserId();
        if (UserId == null)
        {
            return RedirectToAction("Index", "Customer", new { UserId = userId });
        }

        //Trying to get a view model for customer from the received UserId.
        CompanyOverview modelInstance = CompanyOverview.GetCompanyByUser(UserId, db);
        if (modelInstance == null)
        {
            return RedirectToAction("CreateProfile", "Customer");
        }
        return View(modelInstance);
    }

Comments