Last active
April 19, 2022 01:43
-
-
Save scottoffen/d2334113530ecab67f90db0906ed1217 to your computer and use it in GitHub Desktop.
API Token Renewal in ASP.NET 6.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Http.Headers; | |
public interface IExampleClient | |
{ | |
/* interface methods here */ | |
} | |
public class ExampleClient : IExampleClient | |
{ | |
public static string AccessToken { get; set; } | |
private readonly HttpClient _client; | |
public ExampleClient(HttpClient client) | |
{ | |
_client = client; | |
_client.BaseAddress = new Uri("https://api.example.com/"); | |
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken); | |
} | |
/* implement interface */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.DependencyInjection; | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddHttpClient<IExampleClient, ExampleClient>(); | |
// if you want to change the token duration, do it before here | |
// TokenRenewalService.TokenDurationSeconds = 15 * 60; // 15 minutes | |
services.AddHttpClient<TokenRenewalService>(); | |
services.AddHostedService<TokenRenewalService>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Hosting; | |
using FluentHttpClient; | |
public class TokenRenewalService : BackgroundService, IDisposable | |
{ | |
public static int TokenDurationSeconds { get; set; } = 5 * 60; // 5 minutes | |
private readonly HttpClient _client; | |
private readonly FormUrlEncodedContent _content; | |
public TokenRenewalService(HttpClient client) | |
{ | |
_client = client; | |
_client.BaseAddress = new Uri("https://api.example.com/"); | |
_content = new FormUrlEncodedContent(new Dictionary<string, string> | |
{ | |
{"username", "my-username"}, | |
{"password", "my-password"} | |
}); | |
} | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
// Get the token right away! | |
await RenewAccessToken(); | |
// Now set a timer for 10 seconds before our token expires. | |
var timer = new PeriodicTimer(TimeSpan.FromSeconds(TokenDurationSeconds - 10)); | |
// And as long as our timer is running, we'll keep renewing the token! | |
while (await timer.WaitForNextTickAsync(stoppingToken)) | |
{ | |
await RenewAccessToken(); | |
} | |
} | |
private async Task RenewAccessToken() | |
{ | |
var response = await _client.UsingRoute("authenticate") | |
.WithContent(_content) | |
.PostAsync() | |
.GetResponseStringAsync(); | |
ExampleClient.AccessToken = response; | |
} | |
public override void Dispose() | |
{ | |
_client.Dispose(); | |
base.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment