private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Uri authorityUri;
            Uri sharePointTenantUri;
            Uri siteCollectionUri;

            Regex mailRegex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");

            // Input parameters sanity check
            if ((String.IsNullOrEmpty(this.Authority.Text) || !Uri.TryCreate(this.Authority.Text, UriKind.Absolute, out authorityUri)) ||
                (String.IsNullOrEmpty(this.SharePointTenantUri.Text) || !Uri.TryCreate(this.SharePointTenantUri.Text, UriKind.Absolute, out sharePointTenantUri)) ||
                (String.IsNullOrEmpty(this.SiteCollectionUri.Text) || !Uri.TryCreate(this.SiteCollectionUri.Text, UriKind.Absolute, out siteCollectionUri)) ||
                (String.IsNullOrEmpty(this.MailAddressTo.Text) || !mailRegex.IsMatch(this.MailAddressTo.Text)) ||
                (String.IsNullOrEmpty(this.FileToUploadPath.Text) || !File.Exists(this.FileToUploadPath.Text)))
            {
                MessageBoxResult msgBoxResult = MessageBox.Show("Please fill all the input parameters!");
                return;
            }

            try
            {
                PrintHeader("Authentication Phase");
                AuthenticationHelper authenticationHelper = new AuthenticationHelper();
                authenticationHelper.EnsureAuthenticationContext(this.Authority.Text);

                PrintHeader("Discovery API demo");
                DiscoveryHelper discoveryHelper = new DiscoveryHelper(authenticationHelper);
                var             t = await discoveryHelper.DiscoverMyFiles();

                PrintSubHeader("Current user information");
                PrintAttribute("OneDrive URL", t.ServiceEndpointUri);

                var t3 = await discoveryHelper.DiscoverMail();

                PrintAttribute("Mail URL", t3.ServiceEndpointUri);

                PrintHeader("Files API demo");
                // Read all files on your onedrive
                PrintSubHeader("List TOP 20 files and folders in the OneDrive");

                MyFilesHelper myFilesHelper = new MyFilesHelper(authenticationHelper);
                var           allMyFolders  = await myFilesHelper.GetMyFolders();

                var allMyFiles = await myFilesHelper.GetMyFiles();

                foreach (var item in allMyFiles.Take(20))
                {
                    PrintAttribute("URL", item.WebUrl);
                }

                // Upload a file to the "Shared with everyone" folder
                PrintSubHeader("Upload a file to OneDrive");
                if (allMyFolders.Any())
                {
                    await myFilesHelper.UploadFile(this.FileToUploadPath.Text, allMyFolders.First().Id);
                }
                else
                {
                    await myFilesHelper.UploadFile(this.FileToUploadPath.Text);
                }
                // Shared with everyone

                // Iterate over the "Shared with everyone" folder
                PrintSubHeader("List all files and folders in the Shared with everyone folder");
                var myFiles = await myFilesHelper.GetMyFiles(allMyFolders.First().Id);

                foreach (var item in myFiles)
                {
                    PrintAttribute("URL", item.WebUrl);
                }

                PrintHeader("Mail API demo");

                //Get mails
                PrintSubHeader("Retrieve mails from INBOX");
                MailHelper mailHelper = new MailHelper(authenticationHelper);
                var        mails      = await mailHelper.GetMessages();

                PrintSubHeader(String.Format("Printing TOP 10 mails of {0}", mails.Count()));
                foreach (var item in mails.Take(10))
                {
                    PrintAttribute("From ", String.Format("{0} / {1}", item.From != null ? item.From.EmailAddress.Address : "", item.Subject));
                }

                //Send mail
                PrintSubHeader("Send a mail");
                await mailHelper.SendMail(this.MailAddressTo.Text, "Let's Hack-A-Thon - Office365Api.Demo", "This will be <B>fun...</B>");

                //Create message in drafts folder
                PrintSubHeader("Store a mail in the drafts folder");
                await mailHelper.DraftMail(this.MailAddressTo.Text, "Let's Hack-A-Thon - Office365Api.Demo", "This will be fun (in draft folder)...");

                PrintHeader("Active Directory API demo");
                ActiveDirectoryHelper activeDirectoryHelper = new ActiveDirectoryHelper(authenticationHelper);
                var allADUsers = await activeDirectoryHelper.GetUsers();

                PrintSubHeader(String.Format("Printing TOP 10 users of {0}", allADUsers.Count()));
                foreach (var user in allADUsers.Take(10))
                {
                    PrintAttribute("User", user.UserPrincipalName);
                }

                PrintHeader("All done...");
            }
            catch (Exception ex)
            {
                string message = "";
                if (ex is AggregateException)
                {
                    message = ex.InnerException.Message;
                }
                else
                {
                    message = ex.Message;
                }

                PrintException(message);
            }
        }
Beispiel #2
0
        private bool execUserCmd()
        {
            bool Disabled = (!cbEnabled.Checked);

            um.OU_Filter = tbOU.Text.Trim();
            int reqId = 0;

            User   user   = new User(tbUser.Text, tbFirst.Text, tbLast.Text, tbEmail.Text, tbPwd.Text, cbEnabled.Checked);
            string cmd    = (string)cbCommand.SelectedItem;
            string err    = "";
            bool   status = false;

            da.AddRequestQueue(100, cmd, ref reqId, ref err);
            switch (cmd.ToLower())
            {
            case "add":
                status = um.CreateUser(user, 100, reqId, ref err);
                break;

            case "changepassword":
                status = um.ChangeUserPassword(user.Username, user.Password, 100, reqId, ref err);
                break;

            case "delete":
                status = um.DeleteUser(user, 100, reqId, ref err);
                break;

            case "disable":
                status = um.DisableUser(user, 100, reqId, ref err);
                break;

            case "enable":
                status = um.EnableUser(user, 100, reqId, ref err);
                break;

            case "import":
                status = um.ImportUsers(tbOU.Text, 100, reqId, ref err);
                break;

            case "update":
                status = um.UpdateUser(user, 100, reqId, ref err);
                break;

            case "list":
                List <ADUserDetail> userList = adHelper.GetUsers();
                if (userList.Count <= 0)
                {
                    rtbMsg.AppendText("No users found.");
                    return(false);
                }
                foreach (ADUserDetail u in userList)
                {
                    rtbMsg.AppendText("Username: "******", Disabled=" + u.AccountDisabled.ToString() + ", FirstName=" + u.FirstName + ",LastName=" + u.LastName + ", Email=" + u.EmailAddress + "\r\n");
                    rtbMsg.AppendText("===================================================================================================================\r\n");
                }
                break;

            default:
                MessageBox.Show("Unknown command.");
                return(false);
            }
            if (status == false)
            {
                rtbMsg.AppendText(err);
            }
            else
            {
                rtbMsg.AppendText("Successfully completed " + cmd + "!");
            }
            return(true);
        }
Beispiel #3
0
        private bool ImportADUsers(int userId, int reqId, ref string err)
        {
            err = "";
            bool logErr = false;

            try
            {
                List <ADUserDetail> adUserList = null;
                adUserList = m_adHelper.GetUsers();
                if (adUserList == null)
                {
                    err = "ImportADUsers, User List is empty for OU " + m_OU_Filter;
                    int Event_id = 6049;
                    EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
                    return(false);
                }
                if (adUserList.Count <= 0)
                {
                    err = "ImportADUsers, User List is empty for OU ::  " + m_OU_Filter;
                    //int Event_id = 6050;
                    //EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
                    return(false);
                }
                User u      = new User();
                int  total  = adUserList.Count;
                int  proc   = 0;
                int  failed = 0;
                m_dataAccess.UpdateReqProgress(userId, Common.UserMgrCommandType.ImportUsers.ToString(), ref reqId, total, proc, failed, ref err);
                int newUserId = -1;
                foreach (ADUserDetail adUser in adUserList)
                {
                    u.Firstname = adUser.FirstName;
                    u.Lastname  = adUser.LastName;
                    u.Email     = adUser.EmailAddress;
                    u.Username  = adUser.LoginName;
                    u.Enabled   = !adUser.AccountDisabled;
                    try
                    {
                        newUserId = m_dataAccess.UpdateUser(u, ref err);
                        if (newUserId <= 0)
                        {
                            failed++;
                        }
                        proc++;
                    }
                    catch (Exception ex)
                    {
                        failed++;
                        proc++;
                        err = "ImportADUsers, Exception:" + ex.Message;
                        int Event_id = 6051;
                        EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
                    }
                    m_dataAccess.UpdateReqProgress(userId, Common.UserMgrCommandType.ImportUsers.ToString(), ref reqId, total, proc, failed, ref err);
                }

                err = "Successfully imported " + proc + " user accounts from AD OU " + m_OU_Filter;
                Trace.TraceInformation(err);
                int event_id = 2001;
                EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Information, event_id, Category);

                m_dataAccess.UpdateRequestQueue(userId, Common.UserMgrCommandType.ImportUsers.ToString(), ref reqId, true, " ", ref err);
                List <User> userList = m_dataAccess.GetUserAccounts(ref err);
                if (userList == null)
                {
                    return(false);
                }
                if (userList.Count <= 0)
                {
                    return(false);
                }
                int  i;
                bool found = false;
                foreach (User ua in userList)
                {
                    found = false;
                    for (i = 0; i < adUserList.Count; i++)
                    {
                        if (ua.Username == adUserList[i].LoginName)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        m_dataAccess.DisableUser(ua, ref err);
                    }
                }
            }
            catch (Exception ex)
            {
                err = "Failed to import AD users, OU:" + m_OU_Filter + ", Exception:" + ex.Message;
                int Event_id = 6052;
                EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
                return(false);
            }
            finally
            {
                if (logErr)
                {
                    int Event_id = 6053;
                    EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
                }
            }

            return(true);
        }