Skip to content

Commit aef7ff7

Browse files
committed
Add JwtBearer test for SaveToken #1768
1 parent 47caa67 commit aef7ff7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/Microsoft.AspNetCore.Authentication.Test/JwtBearerTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,43 @@ public async Task BearerTokenValidation()
468468
Assert.Equal(HttpStatusCode.OK, response.Response.StatusCode);
469469
}
470470

471+
[Fact]
472+
public async Task SaveBearerToken()
473+
{
474+
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(new string('a', 128)));
475+
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
476+
477+
var claims = new[]
478+
{
479+
new Claim(ClaimTypes.NameIdentifier, "Bob")
480+
};
481+
482+
var token = new JwtSecurityToken(
483+
issuer: "issuer.contoso.com",
484+
audience: "audience.contoso.com",
485+
claims: claims,
486+
expires: DateTime.Now.AddMinutes(30),
487+
signingCredentials: creds);
488+
489+
var tokenText = new JwtSecurityTokenHandler().WriteToken(token);
490+
491+
var server = CreateServer(o =>
492+
{
493+
o.SaveToken = true;
494+
o.TokenValidationParameters = new TokenValidationParameters()
495+
{
496+
ValidIssuer = "issuer.contoso.com",
497+
ValidAudience = "audience.contoso.com",
498+
IssuerSigningKey = key,
499+
};
500+
});
501+
502+
var newBearerToken = "Bearer " + tokenText;
503+
var response = await SendAsync(server, "http://example.com/token", newBearerToken);
504+
Assert.Equal(HttpStatusCode.OK, response.Response.StatusCode);
505+
Assert.Equal(tokenText, await response.Response.Content.ReadAsStringAsync());
506+
}
507+
471508
[Fact]
472509
public async Task SignInThrows()
473510
{
@@ -1140,6 +1177,11 @@ private static TestServer CreateServer(Action<JwtBearerOptions> options = null,
11401177

11411178
await context.Response.WriteAsync(identifier.Value);
11421179
}
1180+
else if (context.Request.Path == new PathString("/token"))
1181+
{
1182+
var token = await context.GetTokenAsync("access_token");
1183+
await context.Response.WriteAsync(token);
1184+
}
11431185
else if (context.Request.Path == new PathString("/unauthorized"))
11441186
{
11451187
// Simulate Authorization failure

0 commit comments

Comments
 (0)