コード例 #1
0
        public async Task <string> GetAccessToken()
        {
            AuthenticationResult result;

            // If there is no saved user account, the user must sign-in
            if (this.userAccount == null)
            {
                try
                {
                    // Invoke device code flow so user can sign-in with a browser
                    result = await this.msalClient.AcquireTokenWithDeviceCode(
                        this.scopes,
                        callback =>
                    {
                        NonBlockingLogger.Info(callback.Message);
                        return(Task.CompletedTask);
                    }).ExecuteAsync();

                    this.userAccount = result.Account;
                    return(result.AccessToken);
                }
                catch (Exception exception)
                {
                    NonBlockingLogger.Error($"Error getting access token: {exception.Message}");
                    return(null);
                }
            }

            // If there is an account, call AcquireTokenSilent
            // By doing this, MSAL will refresh the token automatically if
            // it is expired. Otherwise it returns the cached token.
            result = await this.msalClient.AcquireTokenSilent(this.scopes, this.userAccount).ExecuteAsync();

            return(result.AccessToken);
        }
コード例 #2
0
        public async Task SendMailToAsync(string mailAdress, string subject, string messageBody)
        {
            try
            {
                Message message = new Message
                {
                    Subject = subject,
                    Body    = new ItemBody {
                        ContentType = BodyType.Text, Content = messageBody
                    },
                    ToRecipients = new List <Recipient> {
                        new Recipient {
                            EmailAddress = new EmailAddress {
                                Address = mailAdress
                            }
                        }
                    },
                    Sender = new Recipient {
                        EmailAddress = new EmailAddress {
                            Address = this.fromMailAddress, Name = this.fromName
                        }
                    },
                    From = new Recipient {
                        EmailAddress = new EmailAddress {
                            Address = this.fromMailAddress, Name = this.fromName
                        }
                    }
                };

                await this.graphServiceClient.Me.SendMail(message).Request().PostAsync();
            }
            catch (Exception e)
            {
                NonBlockingLogger.Error($"{e.Message}\nStacktrace: {e.StackTrace}");
            }
        }
コード例 #3
0
 private static void HandleException(Exception exception)
 {
     NonBlockingLogger.Error($"Unhandled exception occured: {exception.Message}\nStacktrace: {exception.StackTrace}");
 }