Two options:
1) Call the existing method, cast the result to
OkObjectResult
, and cast the
Value
property to a
string
:
IActionResult tokenResult = GetToken(username, password);
OkObjectResult okResult = (OkObjectResult)tokenResult;
user.AccessToken = (string)okResult.Value;
OkObjectResult(Object) Class (Microsoft.AspNetCore.Mvc) | Microsoft Docs[
^]
2) Refactor the
GetToken
action to call another method which returns the token string, and call that method:
private string GetTokenCore(string user, string password)
{
var jwt = new JwtToken();
...
string token = new JwtSecurityTokenHandler().WriteToken(retval);
return token;
}
[AllowAnonymous]
[HttpGet("GetToken")]
public IActionResult GetToken(string user, string password)
{
return Ok(GetTokenCore(user, password));
}
[HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
public IActionResult PostUserLogins([FromBody]Users user)
{
...
user.AccessToken = GetTokenCore(username, password);
...
}