Skip to content

Commit

Permalink
[.NET] Custom token expiration feature updates (#55)
Browse files Browse the repository at this point in the history
* added custom token expiration feature

* fixed PR comments

* fixed the last comment
  • Loading branch information
AikoBB authored Oct 10, 2022
1 parent 1f5473e commit a7eeba5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions AccessTokensQuickstart/AccessTokensQuickstart.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Communication.Identity" Version="1.0.1" />
<PackageReference Include="Azure.Communication.Identity" Version="1.2.0" />
<PackageReference Include="Azure.Identity" Version="1.4.1" />
</ItemGroup>

Expand Down
16 changes: 15 additions & 1 deletion AccessTokensQuickstart/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,27 @@ static async System.Threading.Tasks.Task Main(string[] args)
var identity = identityResponse.Value;
Console.WriteLine($"\nCreated an identity with ID: {identity.Id}");

// Issue an access token with the "voip" scope for an identity
// Issue an access token with a validity of 24 hours and the "voip" scope for an identity
var tokenResponse = await client.GetTokenAsync(identity, scopes: new[] { CommunicationTokenScope.VoIP });
var token = tokenResponse.Value.Token;
var expiresOn = tokenResponse.Value.ExpiresOn;
Console.WriteLine($"\nIssued an access token with 'voip' scope that expires at {expiresOn}:");
Console.WriteLine(token);

// Issue an access token with a validity of an hour and the "voip" scope for an identity
TimeSpan tokenExpiresIn = TimeSpan.FromHours(1);
CommunicationTokenScope[] scopes = new[] { CommunicationTokenScope.VoIP };
tokenResponse = await client.GetTokenAsync(identity, scopes, tokenExpiresIn);

// Issue an identity and an access token with a validity of 24 hours and the "voip" scope for the new identity
var identityAndTokenResponse = await client.CreateUserAndTokenAsync(scopes: new[] { CommunicationTokenScope.VoIP });
identity = identityAndTokenResponse.Value.User;
token = identityAndTokenResponse.Value.AccessToken.Token;
expiresOn = identityAndTokenResponse.Value.AccessToken.ExpiresOn;
Console.WriteLine($"\nCreated an identity with ID: {identity.Id}");
Console.WriteLine($"\nIssued an access token with 'voip' scope that expires at {expiresOn}:");
Console.WriteLine(token);

// Refresh access tokens
var identityToRefresh = new CommunicationUserIdentifier(identity.Id);
var refreshTokenResponse = await client.GetTokenAsync(identityToRefresh, scopes: new[] { CommunicationTokenScope.VoIP });
Expand Down

0 comments on commit a7eeba5

Please sign in to comment.