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
I did something similar before in a Web API.
Can you try this in your API method:
Where value1, … valueN are the parameters you pass onto the next API method.
The redirected method stub is then:
(Adjust your parameter types as required.)
You can simply call by parameter and send json string to the action and in that action you can deserialize the Json string.
In the Other Action Your Can simply Deserialize using NewtonSoft.Json if not included you can download from nuget.
Also you can remove async task from your action to load your view after completing API request.
Just add
in place of
Also in action prototype