From a7eeba5bed1eb08369e5ae487a645669a201fff7 Mon Sep 17 00:00:00 2001 From: Aigerim Date: Mon, 10 Oct 2022 17:43:02 +0100 Subject: [PATCH] [.NET] Custom token expiration feature updates (#55) * added custom token expiration feature * fixed PR comments * fixed the last comment --- .../AccessTokensQuickstart.csproj | 4 ++-- AccessTokensQuickstart/Program.cs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/AccessTokensQuickstart/AccessTokensQuickstart.csproj b/AccessTokensQuickstart/AccessTokensQuickstart.csproj index cd9195d8..c413fc35 100644 --- a/AccessTokensQuickstart/AccessTokensQuickstart.csproj +++ b/AccessTokensQuickstart/AccessTokensQuickstart.csproj @@ -1,4 +1,4 @@ - + Exe @@ -6,7 +6,7 @@ - + diff --git a/AccessTokensQuickstart/Program.cs b/AccessTokensQuickstart/Program.cs index 75bbb44b..0e418d36 100644 --- a/AccessTokensQuickstart/Program.cs +++ b/AccessTokensQuickstart/Program.cs @@ -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 });