skip to Main Content

I have one API endpoint for login. Once we enter correct username and password then we are getting following details :

{
    "accessToken": "Some_Value",
    "accessTokenExpireAt": "Some_Value",
    "refreshToken": "Some_Value",
    "userID": "Some_Value"
}

Once user click on Submit button then below code will be executed.

public async Task<ActionResult> Index(LoginModel loginModel)
        {
            using (var client = new HttpClient())
            {
                string url = "URL_ENDPOINT_FOR_LOGIN";
                client.DefaultRequestHeaders.Accept.Clear();

                var response = await client.PostAsJsonAsync(url, loginModel);

                if (response.IsSuccessStatusCode)
                {
                    var result = response.Content.ReadAsStringAsync();

//Here I am getting all values in JSON in result. Now I want to redirect to user to profile page and on that page need to call another API which will fetch all details of that user. In Result I am getting userId.

                }
                else
                {
                    ModelState.AddModelError("", "Wrong details");
                }
            }
            return View(loginModel);
        }

Now I have one more API end point, when we call that then we are getting that user details like firstname, email id and so on.

I need to display that all user details in View, but only if that accessToken is coming.

So I want to know how I can pass this accessToken and userId to another action and all API url from there.

2

Answers


  1. I did something similar before in a Web API.

    Can you try this in your API method:

    return RedirectToAction("RedirectedAction", 
        "ControllerName", 
        new { val = value1, ..., val = valueN });
    

    Where value1, … valueN are the parameters you pass onto the next API method.

    The redirected method stub is then:

    public async Task<ActionResult> RedirectedAction(
        string value1, string value2, ...)
    {
        ...
    }
    

    (Adjust your parameter types as required.)

    Login or Signup to reply.
  2. You can simply call by parameter and send json string to the action and in that action you can deserialize the Json string.

    public async Task<ActionResult> Index(LoginModel loginModel)
        {
            using (var client = new HttpClient())
            {
                string url = "URL_ENDPOINT_FOR_LOGIN";
                client.DefaultRequestHeaders.Accept.Clear();
    
                var response = await client.PostAsJsonAsync(url, loginModel);
    
                if (response.IsSuccessStatusCode)
                {
                    var result = response.Content.ReadAsStringAsync();
                    string JsonString=result.Result;
                    return RedirectToAction("YourAction", new { YourParam= JsonString });
                }
                else
                {
                    ModelState.AddModelError("", "Wrong details");
                }
            }
            return View(loginModel);
        }
    

    In the Other Action Your Can simply Deserialize using NewtonSoft.Json if not included you can download from nuget.

    public ActionResult YourAction(string YourParam)
    {
         YourClass clsObj= JsonConvert.DeserializeObject<YourClass>(JsonString /*YourParam in this case*/);
        // Do your Work Here
    }
    

    Also you can remove async task from your action to load your view after completing API request.
    Just add

    System.Threading.Tasks.Task<HttpResponseMessage> response = client.PostAsJsonAsync(url, loginModel);
    response.Wait();
    

    in place of

    var response = await client.PostAsJsonAsync(url, loginModel);
    

    Also in action prototype

    public ActionResult Index(LoginModel loginModel)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search