【问题标题】:C# ClickOnce Application - User has refused to grant required permissions to the applicationC# ClickOnce 应用程序 - 用户拒绝向应用程序授予所需的权限
【发布时间】:2016-06-17 00:46:56
【问题描述】:

我正在尝试使用 ClickOnce 安装程序从 sourceforge.com 为我的 C# 应用程序添加自动更新;当我单击已添加到应用程序的“检查更新”按钮时,它成功检查并看到有更新,但是当我单击“立即安装”时,出现以下错误:

用户拒绝授予应用所需的权限

我在 StackOverflow 上看到了一个类似的问题,我已经按照公认的答案所说的解决方案做了,但无济于事。

链接到我阅读的另一个 StackOverflow 问题:Clickonce full trust app update failing with TrustNotGrantedException on Windows 8

值得注意的是,我使用的是 Windows 10,而不是 Windows 8,并且它在另一台 Windows 7 计算机上运行,​​但在我的 Windows 10 计算机上无法使用相同的确切代码。另一个问题还提到他们在 Windows XP 和 7 上工作,但不是 8,但是,适用于他们的 Windows 8 解决方案不适用于我的 Windows 10。

堆栈跟踪:

System.Deployment.Application.TrustNotGrantedException: User has refused to grant required permissions to the application.
   at System.Deployment.Application.ApplicationTrust.RequestTrust(SubscriptionState subState, Boolean isShellVisible, Boolean isUpdate, ActivationContext actCtx, TrustManagerContext tmc)
   at System.Deployment.Application.DeploymentManager.DetermineTrustCore(Boolean blocking, TrustParams tp)
   at System.Deployment.Application.DeploymentManager.DetermineTrust(TrustParams trustParams)
   at System.Deployment.Application.ApplicationDeployment.Update()
   at Desktop_Beautifier.Form1.update()
   at Desktop_Beautifier.Form1.btnCheckUpdates_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我的应用程序中的相关代码:

    private void btnCheckUpdates_Click(object sender, EventArgs e)
    {
        if (CheckForUpdateAvailable())
        {
            update();
        }
        else
        {
            MessageBox.Show("No updates are available at this time.");
        }
    }
    private bool CheckForUpdateAvailable()
    {
        Uri updateLocation = ApplicationDeployment.CurrentDeployment.UpdateLocation;

        //Used to use the Clickonce API but we've uncovered a pretty serious bug which results in a COMException and the loss of ability
        //to check for updates. So until this is fixed, we're resorting to a very lo-fi way of checking for an update.

        WebClient webClient = new WebClient();
        webClient.Encoding = Encoding.UTF8;
        string manifestFile = webClient.DownloadString(updateLocation);

        //We have some garbage info from the file header, presumably because the file is a .application and not .xml
        //Just start from the start of the first tag
        int startOfXml = manifestFile.IndexOfAny(new[] { '<' });
        manifestFile = manifestFile.Substring(startOfXml);

        Version version;

        XmlDocument doc = new XmlDocument();

        //build the xml from the manifest
        doc.LoadXml(manifestFile);

        XmlNodeList nodesList = doc.GetElementsByTagName("assemblyIdentity");
        if (nodesList == null || nodesList.Count <= 0)
        {
            throw new XmlException("Could not read the xml manifest file, which is required to check if an update is available.");
        }

        XmlNode theNode = nodesList[0];
        version = new Version(theNode.Attributes["version"].Value);

        if (version > ApplicationDeployment.CurrentDeployment.CurrentVersion)
        {
            // update application
            return true;
        }
        return false;
    }
    private void update()
    {
        UpdatesForm updatesForm = new UpdatesForm();
        DialogResult result = updatesForm.ShowDialog();
        if (result == DialogResult.OK)
        {
            FileIOPermission fp = new FileIOPermission(PermissionState.Unrestricted);
            fp.Assert();

            ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
            try
            {
                var appId = new ApplicationIdentity(ad.UpdatedApplicationFullName); 
                var unrestrictedPerms = new PermissionSet(PermissionState.Unrestricted);
                var appTrust = new ApplicationTrust(appId)
                {
                    DefaultGrantSet = new PolicyStatement(unrestrictedPerms),
                    IsApplicationTrustedToRun = true,
                    Persist = true
                };
                ApplicationSecurityManager.UserApplicationTrusts.Add(appTrust);

                ad.Update();
                MessageBox.Show("The application has been upgraded, and will now restart.");
                Application.Restart();
            }
            catch (DeploymentDownloadException dde)
            {
                MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
                return;
            }
            CodeAccessPermission.RevertAssert();
        }
        else if (result == DialogResult.Cancel)
        {
            updatesForm.Close();
        }
    }

【问题讨论】:

  • 您可以添加指向您在问题中引用的 SO 问题的链接,这将有助于其他人了解您已经尝试过的内容
  • @Mick 好点,补充。
  • 嘿伙计,我已经发布了另一个 SO 问题的答案。它可能会有所帮助!

标签: c# visual-studio installation clickonce truststore


【解决方案1】:

我最近在尝试将安装/更新 URL 从 http 地址更改为 https 并发布更新时遇到了这个问题。所有现有应用程序在尝试安装更新时都会抛出TrustNotGrantedException。一旦我恢复更改,错误就消失了。

【讨论】:

    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2017-09-06
    相关资源
    最近更新 更多