How to return auth Token from .Net Core [Authorize] method

Here’s what I’ve come up with after looking through the suggestions here:

    public IActionResult Login()
        return Challenge(new AuthenticationProperties
            RedirectUri = $"{HttpContext.Request.PathBase.Value}/GetToken"
        }, OpenIdConnectDefaults.AuthenticationScheme);

    [Authorize(AuthenticationSchemes = OpenIdConnectDefaults.AuthenticationScheme)]
    public IActionResult GetToken()
        var token = _contextAccessor.HttpContext.GetTokenAsync(OpenIdConnectDefaults.AuthenticationScheme, "id_token").Result;

        return Ok(new
            Token = token

I combined two APIs. From the perspective of the client app it makes a call to a Login API and gets the token as the result. Behind the scenes the Login API redirects to a second API to get and return the token.


Revisting this after a while in case anyone sees this. I don’t remember exactly how, but I think there was a problem with above approach for me.

In the end I used the OnSignedIn CookieAuthenticationHandler event to intercept the token and returned it with the response in a Cookie. The Login API still returns a Challenge request.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top