IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

How can i get the access token from identityserver4?

Open manjits1991 opened this issue 9 years ago • 7 comments

I am using identityserver4 with my asp.net core and angular2 application and its working great.Now my requirement is bit changed now for mobile applications i want to do login by user entres his/her mobile number and we check if that user with given phone number exist then send One time password(OTP).And now i want to generate access token only based on the phone number and one time password.How can i do this in identityserver4? I have tried https://localhost:44363/connect/token link so i can use my logics in this method but i don't know where this url is hitting.Kindly help me out.Thnaks in advance

manjits1991 avatar Dec 12 '16 17:12 manjits1991

This seems to be a general question about IdentityServer - not a bug report or an issue.

Please use StackOverflow for that. This has the advantage that questions and answers can be easily found by search engines, and that there are more people answering questions than just us.

For IdentityServer3 https://stackoverflow.com/questions/tagged/?tagnames=identityserver3&sort=newest

For IdentityServer4 https://stackoverflow.com/questions/tagged/?tagnames=identityserver4&sort=newest

For commercial support options - visit https://identityserver.io

leastprivilege avatar Dec 12 '16 19:12 leastprivilege

You can get the access token from the HTTP Header:

private static string GetAccessToken(HttpContext httpContext)
{
    var accessToken = (httpContext.Request.Headers as FrameRequestHeaders).HeaderAuthorization;
    var token = accessToken.First().Remove(0, "Bearer ".Length);
    return token;
}

MovGP0 avatar Jul 24 '17 07:07 MovGP0

@MovGP0 May you please elaborate a bit on where exactly your piece of code can be used?

behrooz66 avatar Jan 15 '18 19:01 behrooz66

@behrooz66 usually you don't use this at all, but use a library like IdentityModel instead.

MovGP0 avatar Jan 15 '18 21:01 MovGP0

Simplified @MovGP0 answer (IdentityServer4.AccessTokenValidation 2.7.0 + IdentityModel 3.10.0):

services.AddScoped(provider =>
{
       //
       var httpService = provider.GetService<IHttpContextAccessor>();                
       var token = httpService.HttpContext.Request.Headers["Authorization"][0].Remove(0, "Bearer ".Length);
       var userGuid = Guid.Parse(httpService.HttpContext.User.FindFirst("sub").Value);     
       // use token
 });

JackRus avatar Dec 10 '18 19:12 JackRus

None of above answers worked out for me. Need to use IdentityServer Tools from IDs4 (IssueJwtAsync) Found the right idea & solution here:

http://docs.identityserver.io/en/latest/topics/tools.html https://stackoverflow.com/a/54913136 https://github.com/IdentityServer/IdentityServer4/issues/2884

arashberlin avatar Mar 20 '19 09:03 arashberlin

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 11 '20 18:01 lock[bot]