Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate access token with IdentityServer4 without password

I have created ASP.NET Core WebApi protected with IdentityServer4 using ROPC flow (using this example: https://github.com/robisim74/AngularSPAWebAPI).

How to manually generate access_token from the server without password?

like image 365
Roman Kolesnikov Avatar asked May 25 '17 04:05

Roman Kolesnikov


People also ask

How do I get access token in IdentityServer4?

Get the client's access token back. With the help of the client Id and secret, the client authenticates with the token endpoint. Resource owner password grant type : You can use the Resource Owner Password to request tokens on behalf of a user to send the user name and password to the token endpoint.

Is IdentityServer4 obsolete?

IdentityServer4 support will last until the end of life of . NET Core 3.1 that means till November 2022. In that way, Duende provides new documentation for the fifth service version.

How do I get the refresh token in IdentityServer4?

To get a new access token, you send the refresh token to the token endpoint. This will result in a new token response containing a new access token and its expiration and potentially also a new refresh token depending on the client configuration (see above).

How can I get access token username and password?

Obtaining an Access Token by Using the User Credentials Without a Client Assertion. Using the resource owner password credentials workflow, the OAuth client can obtain an access token by providing the user's credentials (that is the user name and password).


2 Answers

[HttpPost("loginas/{id}")] [Authorize(Roles = "admin")] public async Task<IActionResult> LoginAs(int id, [FromServices] ITokenService TS,      [FromServices] IUserClaimsPrincipalFactory<ApplicationUser> principalFactory,     [FromServices] IdentityServerOptions options) {     var Request = new TokenCreationRequest();                             var User = await userManager.FindByIdAsync(id.ToString());     var IdentityPricipal = await principalFactory.CreateAsync(User);     var IdServerPrincipal = IdentityServerPrincipal.Create(User.Id.ToString(), User.UserName, IdentityPricipal.Claims.ToArray());      Request.Subject = IdServerPrincipal;     Request.IncludeAllIdentityClaims = true;     Request.ValidatedRequest = new ValidatedRequest();     Request.ValidatedRequest.Subject = Request.Subject;     Request.ValidatedRequest.SetClient(Config.GetClients().First());     Request.Resources = new Resources(Config.GetIdentityResources(), Config.GetApiResources());     Request.ValidatedRequest.Options = options;     Request.ValidatedRequest.ClientClaims = IdServerPrincipal.Claims.ToArray();      var Token = await TS.CreateAccessTokenAsync(Request);     Token.Issuer = "http://" + HttpContext.Request.Host.Value;      var TokenValue = await TS.CreateSecurityTokenAsync(Token);     return Ok(TokenValue); } 

For a newly released IdentityServer 2.0.0 the code needs some modifications:

[HttpPost("loginas/{id}")] [Authorize(Roles = "admin")] public async Task<IActionResult> LoginAs(int id, [FromServices] ITokenService TS,      [FromServices] IUserClaimsPrincipalFactory<ApplicationUser> principalFactory,      [FromServices] IdentityServerOptions options) {     var Request = new TokenCreationRequest();     var User = await userManager.FindByIdAsync(id.ToString());     var IdentityPricipal = await principalFactory.CreateAsync(User);     var IdentityUser = new IdentityServerUser(User.Id.ToString());     IdentityUser.AdditionalClaims = IdentityPricipal.Claims.ToArray();     IdentityUser.DisplayName = User.UserName;     IdentityUser.AuthenticationTime = System.DateTime.UtcNow;     IdentityUser.IdentityProvider = IdentityServerConstants.LocalIdentityProvider;     Request.Subject = IdentityUser.CreatePrincipal();     Request.IncludeAllIdentityClaims = true;     Request.ValidatedRequest = new ValidatedRequest();     Request.ValidatedRequest.Subject = Request.Subject;     Request.ValidatedRequest.SetClient(Config.GetClients().First());     Request.Resources = new Resources(Config.GetIdentityResources(), Config.GetApiResources());     Request.ValidatedRequest.Options = options;     Request.ValidatedRequest.ClientClaims = IdentityUser.AdditionalClaims;     var Token = await TS.CreateAccessTokenAsync(Request);     Token.Issuer = HttpContext.Request.Scheme + "://" + HttpContext.Request.Host.Value;     var TokenValue = await TS.CreateSecurityTokenAsync(Token);     return Ok(TokenValue); } 
like image 88
Roman Kolesnikov Avatar answered Sep 22 '22 01:09

Roman Kolesnikov


Use this:
http://docs.identityserver.io/en/latest/topics/tools.html

Use this tool that come with identity server:
Declare it in the constructor, to receive by dependecy injection.
IdentityServer4.IdentityServerTools _identityServerTools

      var issuer = "http://" + httpRequest.Host.Value;       var token = await _identityServerTools.IssueJwtAsync(           30000,           issuer,           new System.Security.Claims.Claim[1]            {               new System.Security.Claims.Claim("cpf", cpf)           }       );  
like image 29
David Madi Avatar answered Sep 22 '22 01:09

David Madi