Example #1
0
        private bool CheckEnsureUser(Microsoft.Office.Interop.Outlook.MailItem mailItem)
        {
            try
            {
                Microsoft.Office.Interop.Outlook.ExchangeUser currentUser = GetCurrentUserInfo(mailItem);
                if (currentUser != null)
                {
                    string siteUrl = Properties.Settings.Default.ClientContextUrl;
                    using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, Properties.Settings.Default.ClientId, Properties.Settings.Default.ClientSecret))
                    {
                        var rootWeb = clientContext.Site.RootWeb;
                        var usr     = rootWeb.EnsureUser(currentUser.PrimarySmtpAddress);
                        clientContext.Load(usr);
                        clientContext.ExecuteQuery();

                        if (usr != null)
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
            catch (Exception ex)
            {
                return(false);

                throw new Exception(ex.ToString());
            }
        }
Example #2
0
        private Microsoft.Office.Interop.Outlook.ExchangeUser GetCurrentUserInfo(Microsoft.Office.Interop.Outlook.MailItem mailItem)
        {
            try
            {
                Microsoft.Office.Interop.Outlook.AddressEntry addrEntry =
                    mailItem.SendUsingAccount.CurrentUser.AddressEntry;

                if (addrEntry.Type == "EX")
                {
                    Microsoft.Office.Interop.Outlook.ExchangeUser currentUser =
                        mailItem.SendUsingAccount.CurrentUser.AddressEntry.GetExchangeUser();

                    if (currentUser != null)
                    {
                        return(currentUser);
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                return(null);

                throw new Exception(ex.ToString());
            }
        }
Example #3
0
        private bool CheckUser(Microsoft.Office.Interop.Outlook.MailItem mailItem)
        {
            try
            {
                Microsoft.Office.Interop.Outlook.ExchangeUser currentUser = GetCurrentUserInfo(mailItem);
                if (currentUser != null)
                {
                    string siteUrl = Properties.Settings.Default.ClientContextUrl;

                    using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, Properties.Settings.Default.ClientId, Properties.Settings.Default.ClientSecret))
                    {
                        GroupCollection collGroup = clientContext.Web.SiteGroups;

                        clientContext.Load(collGroup);

                        clientContext.Load(collGroup,
                                           groups => groups.Include(
                                               group => group.Title,
                                               group => group.Id,
                                               group => group.Users.Include(
                                                   user => user.Title,
                                                   user => user.Email)));

                        clientContext.ExecuteQuery();

                        foreach (Group oGroup in collGroup)
                        {
                            UserCollection collUser = oGroup.Users;

                            foreach (User oUser in collUser)
                            {
                                if (currentUser.PrimarySmtpAddress == oUser.Email)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }


                return(false);
            }
            catch (Exception ex)
            {
                return(false);

                throw new Exception(ex.ToString());
            }
        }