Kolichikov
Kolichikov

Reputation: 3020

How to pass data between redirect requests in Asp.net core while making sure the data is removed?

We've recently made a change where a process now needs the user to be logged in before they can finish it, but because there is complexity in changing the entry point, they can still start the process without being logged in.

So the flow is something like this:

  1. User tries to do Step A
  2. User is not logged in, so is redirected to Login (with their Step A data saved)
  3. User logs in, and is redirected to finish Step A, with all their data

I'm trying to figure out the correct mechanism to use to keep that Step A data. I tried using TempData, but the problem is that TempData seems to persist indefinitely if it is not consumed.

So, if the user doesn't complete the login in step 2, their tempdata is just sitting there forever, which is undesirable (maybe TempData works differently in production, but debugging locally, the unconsumed data in TempData survived a computer crash).

I would essentially like a way to store the data until it is consumed or the user has left the page, or ended the session.

EDIT (for comments): The data is not large, and is all string/int based, so I could put it into the database. Part of the data is essentially the controller/action that I need to redirect to after the login is done, plus parameters that are needed to be passed to it. I'm not sure storing in the DB works though, since as you say, I would delete it after a set time. But if the user leaves and then comes back to the login page within that time, I don't want them to redirect, since that would cause confusion.

Hidden fields don't seem to me to solve the issue (I might be missing something). I have no issue getting the data to the original action, but I need to pass that data on to a different action.

Upvotes: 0

Views: 256

Answers (1)

Erncodez
Erncodez

Reputation: 36

If I understand correctly you want to create a ReturnUrl string. So that once you use it is authorized - they are redirected to the action/page they were trying to perform/access prior to authorization.

  • Go to you [HttpGet] Login()

  • Add a string ReturnUrl as your parameter.

  • Go to your LoginViewModel and add a nullable string property.

    // Example: 
    Public string ReturnUrl { get; set; }
  • Back to your Login() in the controller

  • Create a model :

    // Example:
    var model = New LoginViewModel()
    {
        ReturnUrl = ReturnUrl;
    }
    //Pass it to the View(model).
  • Go to your view

  • Inside your Form create a hidden input field to capture the ReturnUrl value. Which you will send to your [HttpPost] Login().

  • Inside your 'on successful login logic' first check if ReturnUrl is not empty or null.

    // Example:
    if(/* You login condition check... */)
    {
        if(!string.IsNullOrWhiteSpace(LoginViewModel.ReturnUrl))
        {
            return RedirectToPage(LoginViewModel.ReturnUrl);
        }
    };

Hope that helps!

Upvotes: 0

Related Questions