static void DisplayCurrentUserInfo_REST() { HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, restUrlCurrentUser); request.Headers.Add("Authorization", "Bearer " + CustomTokenManager.GetAccessToken()); request.Headers.Add("Accept", "application/json"); HttpResponseMessage response = client.SendAsync(request).Result; if (response.StatusCode != HttpStatusCode.OK) { throw new ApplicationException("Error!!!!!"); } string jsonResult = response.Content.ReadAsStringAsync().Result; Office365User user = JsonConvert.DeserializeObject <Office365User>(jsonResult); Console.WriteLine("Current user info obtained with REST API"); Console.WriteLine("-------------------------------------------"); Console.WriteLine("ID: " + user.id); Console.WriteLine("User Principal Name: " + user.userPrincipalName); Console.WriteLine("Display Name: " + user.displayName); Console.WriteLine("First Name: " + user.givenName); Console.WriteLine("Last Name: " + user.surname); Console.WriteLine("Mail: " + user.mail); Console.WriteLine(); Console.WriteLine(); }
private static Boolean AutodiscoverUrl(ExchangeService service, Office365User user) { Boolean isSuccess = false; try { LoggingManager.WriteToLog("Exchange Online", "Started", string.Empty); MsSqlManager.SaveLogging(new DAL.Logging.LoggingInfo(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), "Exchange Online", "Started", "Exchange Online started")); service.AutodiscoverUrl(user.Account, CallbackMethods.RedirectionUrlValidationCallback); LoggingManager.WriteToLog("Exchange Online", "Connected", string.Empty); MsSqlManager.SaveLogging(new DAL.Logging.LoggingInfo(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), "Exchange Online", "Connected", "Exchange Online connected")); isSuccess = true; } catch (Exception e) { LoggingManager.WriteToLog("AutodiscoverUrl", "Exception", e.Message); Console.WriteLine(e.Message); } return(isSuccess); }
public async Task <ActionResult> Index() { Office365User user = await MicrosoftGraphApiManager.GetCurrentUser(); return(View(user)); }
internal static void Start(string[] args) { try { var applicationSettings = new Properties.Settings(); ServicePointManager.ServerCertificateValidationCallback = CallbackMethods.CertificateValidationCallBack; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1); // Get the information of the account. var user = new Office365User(); service.Credentials = new WebCredentials(user.Account, user.Pwd); // Set the url of server. if (!AutodiscoverUrl(service, user)) { return; } var mailboxMonitorAddress = applicationSettings.Mailbox_Monitor; var moveMailFolderId = GetFolderId(service, "Processed"); var invalidMailFolderId = GetFolderId(service, "Invalid"); var folderView = new FolderView(2); folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly); folderView.Traversal = FolderTraversal.Deep; //var inboxFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Test"); //var inboxResults = service.FindFolders(WellKnownFolderName.Root, inboxFilter, folderView); // var inboxResults = service.FindFolders(WellKnownFolderName.Inbox, folderView); // if (inboxResults.TotalCount > 0) // { Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox); // var inboxId = inboxResults.Folders[0].Id; Folder inboxFolder = Folder.Bind(service, inbox.Id); if (inboxFolder.UnreadCount > 0) { ItemView unreadView = new ItemView(inboxFolder.UnreadCount); unreadView.PropertySet = PropertySet.IdOnly; FindItemsResults <Item> results = service.FindItems(inbox.Id, unreadView); foreach (Item item in results.Items) { EmailMessage email = EmailMessage.Bind(service, new ItemId(item.Id.UniqueId.ToString())); if (!email.IsRead) { PropertySet ps = new PropertySet(ItemSchema.MimeContent, ItemSchema.Body, EmailMessageSchema.Sender, EmailMessageSchema.IsRead, ItemSchema.DateTimeReceived, ItemSchema.Attachments); email.Load(ps); var savedMySQLMessageId = MsSqlManager.SaveRAWMessage(email); var processedPlainTextBody = ProcessEmailBodyText(email, mailboxMonitorAddress, moveMailFolderId, savedMySQLMessageId); if (!processedPlainTextBody) { var processedAttachment = ProcessEmailAttachments(email, mailboxMonitorAddress, moveMailFolderId, savedMySQLMessageId); if (!processedAttachment) { ProcessEmailAsInvalid(email, mailboxMonitorAddress, invalidMailFolderId); } } } } } // } } catch (Exception exc) { MsSqlManager.SaveLogging(new DAL.Logging.LoggingInfo(DateTime.Now.ToString(), "Start", "Exception", exc.StackTrace)); MsSqlManager.SaveLogging(new DAL.Logging.LoggingInfo(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), "Exchange Online", "Exception", exc.StackTrace)); } }