Simple Bearer Token Credential Wrapper for C# (Azure SDK)


The Azure SDK has a TokenCredential Class which is abstract and there are quite a few Token Credential Classes see this full list.

Azure.Identity.AuthorizationCodeCredential
Azure.Identity.AzureCliCredential
Azure.Identity.AzurePowerShellCredential
Azure.Identity.ChainedTokenCredential
Azure.Identity.ClientAssertionCredential
Azure.Identity.ClientCertificateCredential
Azure.Identity.ClientSecretCredential
Azure.Identity.DefaultAzureCredential
Azure.Identity.DeviceCodeCredential
Azure.Identity.EnvironmentCredential
Azure.Identity.InteractiveBrowserCredential
Azure.Identity.ManagedIdentityCredential
Azure.Identity.OnBehalfOfCredential
Azure.Identity.SharedTokenCacheCredential
Azure.Identity.UsernamePasswordCredential
Azure.Identity.VisualStudioCodeCredential
Azure.Identity.VisualStudioCredential
Microsoft.Identity.Web.TokenAcquisitionAppTokenCredential
Microsoft.Identity.Web.TokenAcquisitionTokenCredential

csharp Simple Bearer Token Credential Wrapper for C# (Azure SDK) c # cloud Microsoft Azure

csharp

There is no direct class that we can instantise with a Simple Bearer Token. The reason is that a bearer token is static, and there is no mechanism to renew it.

Here is a quick C# class that derives from the abstract TokenCredential class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class BearerTokenCredential : TokenCredential
{
    /// <summary>
    /// Bearer Token String
    /// </summary>
    private string Token { get; set; }
 
    /// <summary>
    /// Constructor that takes a Bearer Token
    /// </summary>
    /// <param name="token"/>
    public BearerTokenCredential(string token)
    {
        Token = token;
    }
 
    /// <summary>
    /// Return a Bearer Token
    /// </summary>
    /// <param name="requestContext"/>
    /// <param name="cancellationToken"/>
    /// <returns></returns>
    public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
    {
        return new AccessToken(Token, DateTimeOffset.Now.AddDays(1));
    }
 
    /// <summary>
    /// Returns a Bearer Token Asynchronously
    /// </summary>
    /// <param name="requestContext"/>
    /// <param name="cancellationToken"/>
    /// <returns></returns>
    public override ValueTask<accesstoken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
    {
        return new ValueTask<accesstoken>(Task.FromResult(new AccessToken(Token, DateTimeOffset.Now.AddDays(1))));
    }
}
public class BearerTokenCredential : TokenCredential
{
    /// <summary>
    /// Bearer Token String
    /// </summary>
    private string Token { get; set; }

    /// <summary>
    /// Constructor that takes a Bearer Token
    /// </summary>
    /// <param name="token"/>
    public BearerTokenCredential(string token)
    {
        Token = token;
    }

    /// <summary>
    /// Return a Bearer Token
    /// </summary>
    /// <param name="requestContext"/>
    /// <param name="cancellationToken"/>
    /// <returns></returns>
    public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
    {
        return new AccessToken(Token, DateTimeOffset.Now.AddDays(1));
    }

    /// <summary>
    /// Returns a Bearer Token Asynchronously
    /// </summary>
    /// <param name="requestContext"/>
    /// <param name="cancellationToken"/>
    /// <returns></returns>
    public override ValueTask<accesstoken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
    {
        return new ValueTask<accesstoken>(Task.FromResult(new AccessToken(Token, DateTimeOffset.Now.AddDays(1))));
    }
}

As you can probably notice, there Expiry timestamp is not used. We use a placeholder via DateTimeOffset.Now.AddDays(1). The TokenCredential class declares two methods (GetToken and GetTokenAsync) that we override by simply returning the Bearer Token provided in the constructor.

With the BearerTokenCredential, we can now pass this to other C# classes that require a TokenCredential parameter.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
503 words
Last Post: Teaching Kids Programming - Introduction to XML Data Format
Next Post: Teaching Kids Programming - Finding Path Sum in Binary Tree via Recursive Depth First Search Algorithm

The Permanent URL is: Simple Bearer Token Credential Wrapper for C# (Azure SDK)

One Response

  1. xpouyat

Leave a Reply