The Azure.Identity library does not contain a TokenCredential
implementation which can be constructed directly with an AccessToken
. This is intentionally omitted as authenticating a client with a static token is in most cases an anti-pattern, as access tokens expire frequently and have constrained usage. However, there are some scenarios where authenticating a service client with a prefetched token is necessary.
In this example StaticTokenCredential
implements the TokenCredential
abstraction. It takes a prefetched access token in its constructor as a string
or AccessToken
, and simply returns that from its implementation of GetToken
and GetTokenAsync
.
public class StaticTokenCredential : TokenCredential
{
private AccessToken _token;