Skip to content

Instantly share code, notes, and snippets.

@prom3theu5
Created January 26, 2020 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prom3theu5/bf2025d0e4d41a60e088163b3c068bc4 to your computer and use it in GitHub Desktop.
Save prom3theu5/bf2025d0e4d41a60e088163b3c068bc4 to your computer and use it in GitHub Desktop.
Polly AuthRefresh
var authorisationEnsuringPolicy = Policy
.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.Unauthorized)
.RetryAsync(
retryCount: 1, // Consider how many retries. If auth lapses and you have valid credentials, one should be enough; too many tries can cause some auth systems to blacklist.
onRetryAsync: async (outcome, retryNumber, context) => FooRefreshAuthorizationAsync(context),
/* more configuration */);
var response = authorisationEnsuringPolicy.ExecuteAsync(context => DoSomethingThatRequiresAuthorization(context), cancellationToken);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment