我们在C#中有一个winforms clickonce应用程序,该应用程序被授予完全信任并使用有效证书进行了签名。

该应用程序可以在Windows XP,Windows 7上正常运行并正确更新。但是,在Windows 8计算机上,它只是无法更新。该应用程序可以正常运行。但是,第一个升级到更高版本的更新请求失败,并带有:System.Deployment.Application.TrustNotGrantedException
调用ApplicationDeployment::CheckForDetailedUpdate()失败后,代码失败。想知道为什么会发生这种情况,因为在所有早期版本的Windows上都可以运行完全相同的代码。任何帮助将不胜感激。以下是相关的堆栈跟踪:

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.CheckForDetailedUpdate(Boolean persistUpdateCheckResult)
   at System.Deployment.Application.ApplicationDeployment.CheckForDetailedUpdate()

最佳答案

我唯一看到此堆栈跟踪的时间是在我尝试调用CheckForDetailedUpdate()时未事先设置显式信任的情况。添加下面的代码后,更新检查起作用。

// Setup the trust level
var deployment = ApplicationDeployment.CurrentDeployment;
var appId = new ApplicationIdentity(deployment.UpdatedApplicationFullName);
var unrestrictedPerms = new PermissionSet(PermissionState.Unrestricted);
var appTrust = new ApplicationTrust(appId) {
    DefaultGrantSet = new PolicyStatement(unrestrictedPerms),
    IsApplicationTrustedToRun = true,
    Persist = true
};
ApplicationSecurityManager.UserApplicationTrusts.Add(appTrust);

// Check for update
var info = deployment.CheckForDetailedUpdate();

10-07 20:09
查看更多