I'm creating a Windows form application using Graph API. In application, I have more forms. Also, I have a function for logging in the user and when the user logs in his name is written on the label on the first form. On other forms I have a Cancel button, so when the user clicks on the Cancel button, the first form appears, but user name isn't written on the label. Does anyone know how to display user name on the first form when user clicks Cancel button? Here is the code:
- public static class GraphHelper
- {
- private static string[] scopes = new string[] { "user.read" };
- public static string TokenForUser = null;
- public static DateTimeOffset expiration;
-
- private const string ClientId = "599ed98d-4356-4a96-ad37-04391e9c48dc";
-
- private const string Tenant = "common";
- private const string Authority = "https://login.microsoftonline.com/" + Tenant;
-
-
- private static IPublicClientApplication PublicClientApp;
-
- private static string MSGraphURL = "https://graph.microsoft.com/beta/";
- private static AuthenticationResult authResult;
-
- public static GraphServiceClient graphClient;
- public static string token;
-
- public static GraphServiceClient GetGraphClient(string token)
- {
- if (graphClient == null)
- {
-
- try
- {
- graphClient = new GraphServiceClient(
- "https://graph.microsoft.com/beta",
- new DelegateAuthenticationProvider(
- async (requestMessage) =>
- {
- requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
-
- requestMessage.Headers.Add("SampleID", "uwp-csharp-snippets-sample");
-
- }));
- return graphClient;
- }
-
- catch (Exception ex)
- {
- Debug.WriteLine("Could not create a graph client: " + ex.Message);
- }
- }
- return graphClient;
- }
-
- public static async Task<string> GetTokenForUserAsync()
- {
- if (TokenForUser == null || expiration <= DateTimeOffset.UtcNow.AddMinutes(10))
- {
- PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
- .WithAuthority(Authority)
- .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
- .WithLogging((level, message, containsPii) =>
- {
- Debug.WriteLine($"MSAL: {level} {message} ");
- }, LogLevel.Warning, enablePiiLogging: false, enableDefaultPlatformLogging: true)
- .Build();
-
-
- IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync().ConfigureAwait(false);
- IAccount firstAccount = accounts.FirstOrDefault();
-
- try
- {
- authResult = await PublicClientApp.AcquireTokenSilent(scopes, firstAccount)
- .ExecuteAsync();
- }
- catch (MsalUiRequiredException ex)
- {
-
- Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
-
- authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
- .ExecuteAsync()
- .ConfigureAwait(false);
- }
-
- TokenForUser = authResult.AccessToken;
- }
-
- return TokenForUser;
- }
-
- public static async Task<User> GetMeAsync(string token)
- {
- graphClient = GetGraphClient(token);
- try
- {
-
- return await graphClient.Me
- .Request()
- .Select(u => new
- {
- u.DisplayName
- })
- .GetAsync();
- }
- catch (ServiceException ex)
- {
- return null;
- }
- }
- }
-
- public partial class Form1 : Form
- {
- public static string token;
- public static GraphServiceClient graphClient;
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private async void button1_Click(object sender, EventArgs e)
- {
- token = await GraphHelper.GetTokenForUserAsync();
- User graphUser = await GraphHelper.GetMeAsync(token);
- label4.Text = graphUser.DisplayName;
- }
- }
-
- public partial class Form2 : Form
- {
-
- public Form2()
- {
- InitializeComponent();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Form1 f1 = new Form1();
- this.Close();
- f1.Show();
- }
- }