.Net Console App keeps crashing

时光怂恿深爱的人放手 提交于 2019-12-12 06:17:32

问题


I added the Manifest file with the element

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

I built the project and it keeps crashing when a non-administrator runs it.

Unhandled Exception: System.Security.SecurityException: Request for the permissi on of type 'System.Security.Permissions.RegistryPermission, mscorlib, Version=2. 0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMa rk& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at Microsoft.Win32.RegistryKey.CheckSubKeyReadPermission(String subkeyName) at Microsoft.Win32.RegistryKey.CheckOpenSubKeyPermission(String subkeyName, B oolean subKeyWritable) at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) at LotusTrustedSites.RegistryKeyExtensionMethods.GetOrCreateSubKey(RegistryKe y registryKey, String parentKeyLocation, String key, Boolean writable) at LotusTrustedSites.ReportDownloader.Main(String[] args) The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.RegistryPermission

The assembly or AppDomain that failed was: MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null The method that caused the failure was: Microsoft.Win32.RegistryKey GetOrCreateSubKey(Microsoft.Win32.RegistryKey, Syste m.String, System.String, Boolean)


回答1:


Of course it fails. You told it to require administrator access, and non-administrator accounts don't have the ability to provide that. Did you expect it to run with administrator privileges for non-admin users? That would be a major security flaw in Windows if it were possible.

If you need an app to run as an administrator that is accessible to non-admins, the only way I know to do it is to install the app as a scheduled task, and set the task to run using an admin account. Non-admins should still be able to see and run this task, and it will use the supplied admin credentials.




回答2:


You specified requireAdministrator, but you're not running it as an administrator. You should either change it to asInvoker or you should run it as an administrator.




回答3:


As far as I can see, you're trying to obtain some registry permissions. This is the reason it needs administrative privileges.

Try to revisit (or post) the code that deals with registry access and try to figure out what causes the problem



来源:https://stackoverflow.com/questions/6059615/net-console-app-keeps-crashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!