示例#1
0
        public async Task <ActionResult> Upload(HttpPostedFileBase[] files, OverwriteBehaviour overwriteBehaviour, string clientId)
        {
            SignalRMessage signalR = new SignalRMessage(clientId);

            supportedFolders.Add("WindowsAutopilotDeploymentProfile");
            supportedFolders.Add("DeviceConfiguration");
            supportedFolders.Add("DeviceCompliancePolicy");
            supportedFolders.Add("DeviceManagementScript");
            supportedFolders.Add("ManagedAppPolicy");
            supportedFolders.Add("RoleDefinition");

            try
            {
                GraphIntuneImport graphIntuneImport = new GraphIntuneImport(clientId, overwriteBehaviour);

                if (files.Length > 0 && files[0].FileName.Contains(".json"))
                {
                    foreach (HttpPostedFileBase file in files)
                    {
                        try
                        {
                            BinaryReader b       = new BinaryReader(file.InputStream);
                            byte[]       binData = b.ReadBytes(file.ContentLength);
                            string       result  = Encoding.UTF8.GetString(binData);
                            await graphIntuneImport.AddIntuneConfig(result);
                        }
                        catch (Exception e)
                        {
                            signalR.sendMessage("Error: " + e.Message);
                        }
                    }
                }
                else if (files.Length > 0 && files[0].FileName.Contains(".zip"))
                {
                    try
                    {
                        Dictionary <string, string> importFiles = new Dictionary <string, string>();
                        MemoryStream target = new MemoryStream();
                        files[0].InputStream.CopyTo(target);
                        byte[] data = target.ToArray();

                        using (var zippedStream = new MemoryStream(data))
                        {
                            using (var archive = new ZipArchive(zippedStream))
                            {
                                foreach (var entry in archive.Entries)
                                {
                                    try
                                    {
                                        if (entry != null)
                                        {
                                            using (var unzippedEntryStream = entry.Open())
                                            {
                                                using (var ms = new MemoryStream())
                                                {
                                                    unzippedEntryStream.CopyTo(ms);
                                                    var    unzippedArray = ms.ToArray();
                                                    string result        = Encoding.UTF8.GetString(unzippedArray);

                                                    if (!string.IsNullOrEmpty(result))
                                                    {
                                                        // Check if key exists
                                                        // Only the case because of enrollment restrictions coming with no value
                                                        if (importFiles.ContainsKey(entry.FullName))
                                                        {
                                                            Random r = new Random();
                                                            importFiles.Add(entry.FullName + r.Next(), result);
                                                        }
                                                        else
                                                        {
                                                            importFiles.Add(entry.FullName, result);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        signalR.sendMessage("Error: " + e.Message);
                                    }
                                }
                            }
                        }

                        // First create all scope tags
                        foreach (KeyValuePair <string, string> entry in importFiles)
                        {
                            if (entry.Key.Contains("RoleScopeTags"))
                            {
                                await graphIntuneImport.AddIntuneScopeTag(entry.Value);
                            }
                        }

                        // Process all remaining intune objects
                        foreach (KeyValuePair <string, string> entry in importFiles)
                        {
                            // Verify folder name
                            if (supportedFolders.Contains(entry.Key.Split('\\')[0]))
                            {
                                await graphIntuneImport.AddIntuneConfig(entry.Value);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        signalR.sendMessage("Error: " + e.Message);
                    }
                }
                else if (files.Length > 0)
                {
                    signalR.sendMessage("Error unsupported file: " + files[0].FileName);
                }
            }
            catch (Exception e)
            {
                signalR.sendMessage("Error: " + e.Message);
            }

            signalR.sendMessage("Done#!");
            return(new HttpStatusCodeResult(204));
        }
示例#2
0
        public async System.Threading.Tasks.Task <ActionResult> Upload(HttpPostedFileBase[] files, OverwriteBehaviour overwriteBehaviour, string clientId)
        {
            SignalRMessage signalR = new SignalRMessage(clientId);

            try
            {
                GraphIntuneImport graphIntuneImport = new GraphIntuneImport(clientId, overwriteBehaviour);

                if (files.Length > 0 && files[0].FileName.Contains(".json"))
                {
                    foreach (HttpPostedFileBase file in files)
                    {
                        try
                        {
                            BinaryReader b       = new BinaryReader(file.InputStream);
                            byte[]       binData = b.ReadBytes(file.ContentLength);
                            string       result  = Encoding.UTF8.GetString(binData);
                            await graphIntuneImport.AddIntuneConfig(result);
                        }
                        catch (Exception e)
                        {
                            signalR.sendMessage("Error: " + e.Message);
                        }
                    }
                }
                else if (files.Length > 0 && files[0].FileName.Contains(".zip"))
                {
                    try
                    {
                        MemoryStream target = new MemoryStream();
                        files[0].InputStream.CopyTo(target);
                        byte[] data = target.ToArray();

                        using (var zippedStream = new MemoryStream(data))
                        {
                            using (var archive = new ZipArchive(zippedStream))
                            {
                                foreach (var entry in archive.Entries)
                                {
                                    try
                                    {
                                        if (entry != null)
                                        {
                                            if (entry.FullName.Contains("WindowsAutopilotDeploymentProfile") || entry.FullName.Contains("DeviceConfiguration") || entry.FullName.Contains("DeviceCompliancePolicy") || entry.FullName.Contains("DeviceManagementScript") || entry.FullName.Contains("ManagedAppPolicy"))
                                            {
                                                using (var unzippedEntryStream = entry.Open())
                                                {
                                                    using (var ms = new MemoryStream())
                                                    {
                                                        unzippedEntryStream.CopyTo(ms);
                                                        var    unzippedArray = ms.ToArray();
                                                        string result        = Encoding.UTF8.GetString(unzippedArray);

                                                        if (!string.IsNullOrEmpty(result))
                                                        {
                                                            await graphIntuneImport.AddIntuneConfig(result);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        signalR.sendMessage("Error: " + e.Message);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        signalR.sendMessage("Error: " + e.Message);
                    }
                }
                else if (files.Length > 0)
                {
                    signalR.sendMessage("Error unsupported file: " + files[0].FileName);
                }
            }
            catch (Exception e)
            {
                signalR.sendMessage("Error: " + e.Message);
            }

            signalR.sendMessage("Done#!");
            return(new HttpStatusCodeResult(204));
        }