Get id_token from MVC backend to in MVC or Razor page

Viewed 25

We are building MVC and Razor page based .net core applications and we want to use the id_token from the open-id-connect IDP to be used in Vue compnents. What is the best way to retrieve and use the token.

I have come up with using the [Authorize] attribute and then get the id_token from the httpcontext but how to reuse the token in a vue component which uses Ajax to access an api which needs the idtoken to identify the user?

My first guess is to add the token to the viewmodel or page model and then render a script which exposes the token to be used front-end, in javascript:

  IdentityToken = HttpContext.GetTokenAsync("id_token").GetAwaiter().GetResult();

and

<script>
    var apiConnectionSettings = {
        "id_token": "@Model.IdentityToken",
    };
</script>

Is there another (better) way to retrieve the token in a mixed MVC/Razor pages and Vue environment?

1 Answers
Related