示例#1
0
        public ActionResult Edit(string id, ClickOnceAppInfo model, bool disclosePublisher)
        {
            var result = GetMyAppInfo(id);

            if (result is ActionResult)
            {
                return(result as ActionResult);
            }

            if (ModelState.IsValid == false)
            {
                return(View(model));
            }

            var appInfo = result as ClickOnceAppInfo;

            appInfo.Title       = model.Title;
            appInfo.Description = model.Description;
            appInfo.ProjectURL  = model.ProjectURL;
            SetupPublisherInformtion(disclosePublisher, appInfo);

            this.ClickOnceFileRepository.SaveAppInfo(id, appInfo);

            return(RedirectToAction("MyApps", "Home"));
        }
        private byte[] UpdateCertificateInfo(ClickOnceAppInfo appInfo)
        {
            appInfo.SignedByPublisher = false;

            var certBin = default(byte[]);
            var tmpPath = default(string);

            try
            {
                tmpPath = ExtractEntryPointCommandFile(appInfo.Name);
                var cert = X509Certificate.CreateFromSignedFile(tmpPath);
                if (cert != null)
                {
                    certBin = cert.GetRawCertData();
                    this.ClickOnceFileRepository.SaveFileContent(appInfo.Name, ".cer", certBin);

                    if (appInfo.PublisherName != null)
                    {
                        var sshPubKeyStr = CertificateValidater.GetSSHPubKeyStrFromGitHubAccount(appInfo.PublisherName);
                        appInfo.SignedByPublisher = CertificateValidater.EqualsPublicKey(sshPubKeyStr, cert);
                    }
                }
            }
            catch (CryptographicException) { }
            finally { if (tmpPath != null)
                      {
                          System.IO.File.Delete(tmpPath);
                      }
            }

            appInfo.HasCodeSigning = certBin != null;
            return(certBin);
        }
        public List <ClickOnceAppInfo> GetCurrentApplications()
        {
            List <ClickOnceAppInfo> apps = new List <ClickOnceAppInfo>();

            foreach (string appKeyName in GetApplications())
            {
                using (RegistryKey key = this.ApplicationsRegistryKey.OpenSubKey(appKeyName, false))
                {
                    string appDir = appKeyName;

                    foreach (string entryName in this.ApplicationsRegistryKey.OpenSubKey($"{appKeyName}", false).GetValueNames())
                    {
                        if (entryName.ToLower().Equals("identity"))
                        {
                            byte[]        data     = (byte[])this.ApplicationsRegistryKey.OpenSubKey($"{appKeyName}", false).GetValue("identity", new byte[] { });
                            StringBuilder identity = new StringBuilder();
                            foreach (byte charByte in data)
                            {
                                identity = identity.Append(Convert.ToChar(charByte));
                            }

                            string appPath = $"{GetInstallationAppPath()}{appDir}";
                            if (Directory.Exists(appPath))
                            {
                                ClickOnceAppInfo appInfo = new ClickOnceAppInfo(identity.ToString());

                                foreach (string executable in _appsExecution.Values)
                                {
                                    string[] appExecutables = Directory.GetFiles(appPath, executable, SearchOption.TopDirectoryOnly);

                                    if (appExecutables.Length > 0)
                                    {
                                        appInfo.ApplicationExecutable = appExecutables[0];

                                        apps.Add(appInfo);
                                        break;
                                    }
                                }
                            }

                            break;
                        }
                    }
                }
            }

            return(apps);
        }
示例#4
0
 private void SetupPublisherInformtion(bool disclosePublisher, ClickOnceAppInfo appInfo)
 {
     if (disclosePublisher)
     {
         var gitHubUserName = User.Identity.GetUserName();
         appInfo.PublisherName           = gitHubUserName;
         appInfo.PublisherURL            = "https://github.com/jsakamoto" + gitHubUserName;
         appInfo.PublisherAvatorImageURL = "https://avatars.githubusercontent.com/" + gitHubUserName;
     }
     else
     {
         appInfo.PublisherName           = null;
         appInfo.PublisherURL            = null;
         appInfo.PublisherAvatorImageURL = null;
     }
 }
        public ActionResult Edit(string id, ClickOnceAppInfo model, bool disclosePublisher)
        {
            var result = GetMyAppInfo(id);

            if (result is ActionResult)
            {
                return(result as ActionResult);
            }

            if (ModelState.IsValid == false)
            {
                return(View(model));
            }

            var appInfo = result as ClickOnceAppInfo;

            appInfo.Title       = model.Title;
            appInfo.Description = model.Description;
            appInfo.ProjectURL  = model.ProjectURL;
            SetupPublisherInformtion(disclosePublisher, appInfo);

            appInfo.SignedByPublisher = false;
            if (appInfo.HasCodeSigning == null)
            {
                UpdateCertificateInfo(appInfo);
            }
            else if (appInfo.HasCodeSigning == true && appInfo.PublisherName != null)
            {
                var tmpPath = Server.MapPath($"~/App_Data/{Guid.NewGuid():N}.cer");
                try
                {
                    var certBin = this.ClickOnceFileRepository.GetFileContent(id, ".cer");
                    System.IO.File.WriteAllBytes(tmpPath, certBin);
                    var sshPubKeyStr = CertificateValidater.GetSSHPubKeyStrFromGitHubAccount(appInfo.PublisherName);
                    appInfo.SignedByPublisher = CertificateValidater.EqualsPublicKey(sshPubKeyStr, tmpPath);
                }
                finally { System.IO.File.Delete(tmpPath); }
            }

            this.ClickOnceFileRepository.SaveAppInfo(id, appInfo);

            var from = Request.QueryString["from"];

            return(from == "detail" ? RedirectToRoute("Detail", new { appId = appInfo.Name }) : RedirectToAction("MyApps", "Home"));
        }
        private byte[] GetCertificateCore(ClickOnceAppInfo appInfo)
        {
            if (appInfo == null)
            {
                return(null);
            }

            if (appInfo.HasCodeSigning == null)
            {
                var certBin = UpdateCertificateInfo(appInfo);
                this.ClickOnceFileRepository.SaveAppInfo(appInfo.Name, appInfo);
                return(certBin);
            }

            return(appInfo.HasCodeSigning == true?
                   this.ClickOnceFileRepository.GetFileContent(appInfo.Name, ".cer") :
                       null);
        }
        private List <ClickOnceAppInfo> GetCurrentPackages()
        {
            List <ClickOnceAppInfo> apps = new List <ClickOnceAppInfo>();

            foreach (string packageKeyName in GetPackages())
            {
                using (RegistryKey key = this.PackagesRegistryKey.OpenSubKey(packageKeyName, false))
                {
                    foreach (string subKey in key.GetSubKeyNames())// Registry.ClassesRoot.OpenSubKey(String.Format(@"{0}\{1}", ClickOnceManager.RegistryRootPath, "PackageMetadata", subKeyName)).GetSubKeyNames())
                    {
                        foreach (string entryName in this.PackagesRegistryKey.OpenSubKey(String.Format(@"{0}\{1}", packageKeyName, subKey), false).GetValueNames())
                        {
                            if (entryName.EndsWith("!DeploymentSourceUri"))
                            {
                                byte[]        data     = (byte[])this.PackagesRegistryKey.OpenSubKey(String.Format(@"{0}\{1}", packageKeyName, subKey), false).GetValue("appid", new byte[] { });
                                StringBuilder identity = new StringBuilder();
                                foreach (byte charByte in data)
                                {
                                    identity = identity.Append(Convert.ToChar(charByte));
                                }

                                string appPath = String.Format("{0}{1}", GetInstallationAppPath(), subKey);
                                if (Directory.Exists(appPath))
                                {
                                    ClickOnceAppInfo appInfo        = new ClickOnceAppInfo(identity.ToString());
                                    string[]         appExecutables = Directory.GetFiles(appPath, _appsExecution[appInfo.Name.ToLower()], SearchOption.TopDirectoryOnly);

                                    if (appExecutables.Length > 0)
                                    {
                                        appInfo.ApplicationExecutable = appExecutables[0];
                                    }

                                    apps.Add(appInfo);
                                }

                                break;
                            }
                        }
                    }
                }
            }

            return(apps);
        }
        /// <summary>
        /// Download the ClickOnce application.
        /// </summary>
        /// <param name="applicationInstallUrl">The installation url of the application.</param>
        /// <param name="useCurrent">If true, use the current url to download. else, use the url given by <paramref name="applicationInstallUrl"/> parameter.</param>
        /// <returns><typeparamref name="ClickOnceAppInfo"/> that holds the information on the installed ClickOnce application.</returns>
        public UTF.TestTools.ClickOnceAppInfo DownloadApplication(Uri applicationInstallUrl = null, bool useCurrent = false)
        {
            ClickOnceAppInfo appInfo = null;

            if ((this.AppInstallationUrl == null) && (applicationInstallUrl == null))
            {
                throw new ArgumentNullException("applicationInstallUrl", "the application url cannot be null.");
            }

            if (useCurrent && (this.AppInstallationUrl == null))
            {
                throw new ArgumentException($"the useCurrent cannot be true because the current url is null.");
            }

            if ((!useCurrent) && (applicationInstallUrl == null))
            {
                throw new ArgumentException($"the useCurrent cannot be false because applicationInstallUrl parameter is null.");
            }

            if (!useCurrent)
            {
                if (this.Manifest != null)
                {
                    Close();
                }

                this.AppInstallationUrl = applicationInstallUrl;
                GetManifest();
            }
            else
            {
                if (this.Manifest == null)
                {
                    GetManifest();
                }
            }

            _appId = null;

            if (this.Manifest == null)
            {
                throw new InvalidOperationException("cannot download application without gettting the manifest first.");
            }

            _appDownloadedSignal = new AutoResetEvent(true);
            _appDownloadedSignal.Reset();
            this.DeploymentManager.DownloadProgressChanged += DeployManager_DownloadProgressChanged;
            //this.DeploymentManager.DownloadProgressChanged += new EventHandler<DownloadProgressChangedEventArgs>(DeployManager_DownloadProgressChanged);
            this.DeploymentManager.DownloadApplicationCompleted += DeployManager_DownloadApplicationCompleted;
            //this.DeploymentManager.DownloadApplicationCompleted += new EventHandler<DownloadApplicationCompletedEventArgs>(DeployManager_DownloadApplicationCompleted);
            this.DeploymentManager.DownloadApplicationAsync();
            _appDownloadedSignal.WaitOne();

            if (this.LastError != null)
            {
                throw new OperationCanceledException("failed in executing ClickOnce GetManifest method.", this.LastError);
            }

            appInfo = new ClickOnceAppInfo(this.Manifest.ApplicationIdentity.FullName); //GetApplication(this.Manifest.ApplicationIdentity.FullName);

            return(appInfo);
        }
        public ActionResult Register(HttpPostedFileBase zipedPackage)
        {
            var userId = User.GetHashedUserId();

            if (userId == null)
            {
                throw new Exception("hashed user id is null.");
            }

            if (ModelState.IsValid == false)
            {
                return(View());
            }

            try
            {
                var tmpPath = Server.MapPath($"~/App_Data/{userId}-{Guid.NewGuid():N}.zip");
                zipedPackage.SaveAs(tmpPath);
                using (var fs = new FileStream(tmpPath, FileMode.Open, FileAccess.Read))
                    using (var zip = new ZipArchive(fs))
                    {
                        // Validate files structure that are included in a .zip file.
                        var appFile = zip.Entries
                                      .Where(e => Path.GetExtension(e.FullName).ToLower() == ".application")
                                      .OrderBy(e => e.FullName.Length)
                                      .FirstOrDefault();
                        if (appFile == null)
                        {
                            return(Error("The .zip file you uploaded did not contain .application file."));
                        }
                        if (Path.GetDirectoryName(appFile.FullName) != "")
                        {
                            return(Error("The .zip file you uploaded contain .application file, but it was not in root of the .zip file."));
                        }

                        // Validate app name does not conflict.
                        var appName = Path.GetFileNameWithoutExtension(appFile.FullName);
                        var success = this.ClickOnceFileRepository.GetOwnerRight(userId, appName);
                        if (success == false)
                        {
                            return(Error("Sorry, the application name \"{0}\" was already registered by somebody else.", appName));
                        }

                        var appInfo = this.ClickOnceFileRepository.GetAppInfo(appName);
                        if (appInfo == null)
                        {
                            appInfo = new ClickOnceAppInfo
                            {
                                Name    = appName,
                                OwnerId = userId
                            };
                        }
                        appInfo.RegisteredAt = DateTime.UtcNow;
                        this.ClickOnceFileRepository.ClearUpFiles(appName);

                        foreach (var item in zip.Entries.Where(_ => _.Name != ""))
                        {
                            var buff = new byte[item.Length];
                            using (var reader = item.Open())
                            {
                                reader.Read(buff, 0, buff.Length);
                                this.ClickOnceFileRepository.SaveFileContent(appName, item.FullName, buff);
#if !DEBUG
                                if (Path.GetExtension(item.FullName).ToLower() == ".application")
                                {
                                    var error = CheckCodeBaseUrl(appName, buff);
                                    if (error != null)
                                    {
                                        return(error);
                                    }
                                }
#endif
                            }
                        }

                        // Sweep temporary file if success.
                        try { System.IO.File.Delete(tmpPath); }
                        catch (Exception) { }

                        // Update certificate information.
                        this.UpdateCertificateInfo(appInfo);

                        this.ClickOnceFileRepository.SaveAppInfo(appName, appInfo);

                        return(RedirectToAction("Edit", new { id = appName }));
                    }
            }
            catch (System.IO.InvalidDataException)
            {
                return(Error("The file you uploaded looks like invalid Zip format."));
            }
        }