microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c92c1919ec3b5a0b25a067ad4ad46da598a2fdca

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/test/ABSTokenServiceClient/UserTokenCLIService.cs

76lines · modecode

1using System.Text.Json;
2using Microsoft.Teams.Bot.Core;
3using Microsoft.Extensions.Hosting;
4using Microsoft.Extensions.Logging;
5
6namespace ABSTokenServiceClient
7{
8 [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1848:Use the LoggerMessage delegates", Justification = "<Pending>")]
9 internal class UserTokenCLIService(UserTokenClient userTokenClient, ILogger<UserTokenCLIService> logger) : IHostedService
10 {
11 public Task StartAsync(CancellationToken cancellationToken)
12 {
13 return ExecuteAsync(cancellationToken);
14 }
15
16 public Task StopAsync(CancellationToken cancellationToken)
17 {
18 return Task.CompletedTask;
19 }
20
21 protected async Task ExecuteAsync(CancellationToken cancellationToken)
22 {
23 const string userId = "your-user-id";
24 const string connectionName = "graph";
25 const string channelId = "msteams";
26
27 logger.LogInformation("Application started");
28
29 try
30 {
31 logger.LogInformation("=== Testing GetTokenStatus ===");
32 GetTokenStatusResult[] tokenStatus = await userTokenClient.GetTokenStatusAsync(userId, channelId, null, cancellationToken);
33 logger.LogInformation("GetTokenStatus result: {Result}", JsonSerializer.Serialize(tokenStatus, new JsonSerializerOptions { WriteIndented = true }));
34
35 if (tokenStatus[0].HasToken == true)
36 {
37 GetTokenResult? tokenResponse = await userTokenClient.GetTokenAsync(userId, connectionName, channelId, null, cancellationToken);
38 logger.LogInformation("GetToken result: {Result}", JsonSerializer.Serialize(tokenResponse, new JsonSerializerOptions { WriteIndented = true }));
39 }
40 else
41 {
42 GetSignInResourceResult req = await userTokenClient.GetSignInResource(userId, connectionName, channelId, null, cancellationToken);
43 logger.LogInformation("GetSignInResource result: {Result}", JsonSerializer.Serialize(req, new JsonSerializerOptions { WriteIndented = true }));
44
45 Console.WriteLine("Code?");
46 string code = Console.ReadLine()!;
47
48 GetTokenResult? tokenResponse2 = await userTokenClient.GetTokenAsync(userId, connectionName, channelId, code, cancellationToken);
49 logger.LogInformation("GetToken With Code result: {Result}", JsonSerializer.Serialize(tokenResponse2, new JsonSerializerOptions { WriteIndented = true }));
50 }
51
52 Console.WriteLine("Want to signout? y/n");
53 string yn = Console.ReadLine()!;
54 if ("y".Equals(yn, StringComparison.OrdinalIgnoreCase))
55 {
56 try
57 {
58 await userTokenClient.SignOutUserAsync(userId, connectionName, channelId, cancellationToken);
59 logger.LogInformation("SignOutUser completed successfully");
60 }
61 catch (Exception ex)
62 {
63 logger.LogError(ex, "Error during SignOutUser");
64 }
65 }
66 }
67 catch (Exception ex)
68 {
69
70 logger.LogError(ex, "Error during API testing");
71 }
72
73 logger.LogInformation("Application completed successfully");
74 }
75 }
76}
77