1

I'm trying to get the logged in users access token from the HttpContext in my Api I'm using .Net-Core 2.1 :

[HttpGet]
public async Task<bool> Test()
{
    var token = await HttpContext.GetTokenAsync("access_token");
    return true;
}

Edit

I'm using the signing manager to show Auth Providers:

SignInManager.GetExternalAuthenticationSchemesAsync()

And in the External Login Callback I store My tokens with the signin manager like so:

var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
if (result.Succeeded)
{
    await _signInManager.UpdateExternalAuthenticationTokensAsync(info);
    _logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider);
    return RedirectToLocal(returnUrl);
}

Authentication Configuration is setup like so:

services.AddAuthentication(COOKIE_AUTH)
    .AddCookie(options => options.ExpireTimeSpan = TimeSpan.FromMinutes(60))
    .AddCoinbase(options => {
        options.SendLimitAmount = 1;
        options.SendLimitCurrency = "USD";
        options.SendLimitPeriod = SendLimitPeriod.day;
        options.ClientId = Configuration["Coinbase:ClientId"];
        options.ClientSecret = Configuration["Coinbase:ClientSecret"];
        COINBASE_SCOPES.ForEach(scope => options.Scope.Add(scope));
        options.SaveTokens = true;
        options.ClaimActions.MapJsonKey("urn:coinbase:avatar", "avatar_url");
    });

When Even I try to obtain the access token I receive null. However I can see that i'm logged in from the HttpContext.User.

How do I obtain my access token from the HttpContext?

2
  • Does this help?: stackoverflow.com/a/49773344/1202807 Commented Nov 25, 2018 at 3:21
  • @GabrielLuci thanks the addcoinbase method inherits from something similar, the weird thing is I have it working in another project Commented Nov 25, 2018 at 4:37

2 Answers 2

3

Can you try with this code.

HttpContext.Request.Headers["authorization"]
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think that is the access token, I think its hidden inside of the cookie some where, but thanks I'll poke around there
0

For some reason when I use the signin manger to login, It doesn't set the tokens on the HttpContext. So Instead I get the access token like so:

[HttpGet]
public async Task<bool> Test()
{
    var userFromManager = await _userManager.GetUserAsync(User);
    var externalAccessToken = await _userManager.GetAuthenticationTokenAsync(
                                   userFromManager, "Coinbase", "access_token");

    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.