如何启用对注册表项“ HKEY_CURRENT_USER \ Software \ myAppKey \ subdir”的ASP.NET写访问权限?

时间:2018-07-07 02:05:04

标签: c# asp.net windows registry permission-denied

在我的ASP.NET Web应用程序My.Computer.Registry.SetValue(Key_Name,Value_Name,Metric)中导致“拒绝访问注册表项”错误。
应用程序正在Azure Windows Server 2012 VM上运行。 但是,我的.NET Windows应用程序也运行在相同的VM上,写入注册表正常。

我尝试设置regedit>密钥:所有人=完全控制,仍然被拒绝。

我在同一台计算机(Azure Windows Server 2012 R2)上的.NET Windows应用程序写入注册表项“ HKEY_CURRENT_USER \ Software \”和SOFTWARE_PROGRAM_NAME _         &“ \ Metrics \”&Key_Name没问题。

这是引起问题的ASP.NET代码...…………..

' Set the key address within the System Registry:
Key_Name = "HKEY_CURRENT_USER\Software\" & SOFTWARE_PROGRAM_NAME _
    & "\Metrics\" & Key_Name

Metric = My.Computer.Registry.GetValue(Key_Name, Value_Name, 0)  ' read present value
if metric_function = 1
    metric = 0
else if metric_function = 2
    Metric = metric_value
else
    Metric += 1                                                      ' increment it
End If
My.Computer.Registry.SetValue(Key_Name, Value_Name, Metric)      ' store new value

浏览ASP.NET WEB时,这里出现错误.....

Server Error in '/' Application.

Access to the registry key 'HKEY_CURRENT_USER\Software\myApp\Metrics\WEB Communication' is denied. 
Exception Details: System.UnauthorizedAccessException: Access to the registry key 'HKEY_CURRENT_USER\Software\Pray\Metrics\WEB Communication' is denied. 
Source Error: 


Line 152:               Metric += 1                                                      ' increment it
Line 153:           End If
Line 154:           My.Computer.Registry.SetValue(Key_Name, Value_Name, Metric)      ' store new value
Line 155:
Line 156:       ' Update the metrics display in the control pane:

Source File: C:\PRIMARY\INTERNET\APPS\DEPLOYED\myApp\myApp web\App_Code\Metrics.vb    Line: 154 


Stack Trace: 


[UnauthorizedAccessException: Access to the registry key 'HKEY_CURRENT_USER\Software\myApp\Metrics\WEB Communication' is denied.]
   Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str) +144
   Microsoft.Win32.RegistryKey.CreateSubKeyInternal(String subkey, RegistryKeyPermissionCheck permissionCheck, Object registrySecurityObj, RegistryOptions registryOptions) +14824601
   Microsoft.Win32.RegistryKey.CreateSubKey(String subkey) +25
   Microsoft.Win32.Registry.SetValue(String keyName, String valueName, Object value, RegistryValueKind valueKind) +78
   Microsoft.VisualBasic.MyServices.RegistryProxy.SetValue(String keyName, String valueName, Object value) +48
   Metrics.inc(Object& Key_Name, Object& Value_Name, Object metric_function, Object metric_value) in C:\PRIMARY\INTERNET\APPS\DEPLOYED\myApp\myApp web\App_Code\Metrics.vb:154
   Metrics.Communication(Object& Value_Name, Object reset_metric) in C:\PRIMARY\INTERNET\APPS\DEPLOYED\myApp\myApp web\App_Code\Metrics.vb:88
   _Default.Page_Load(Object sender, EventArgs e) in C:\PRIMARY\INTERNET\APPS\DEPLOYED\myApp\myApp web\Default.aspx.vb:142
   System.Web.UI.Control.OnLoad(EventArgs e) +103
   System.Web.UI.Control.LoadRecursive() +68
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1381

1 个答案:

答案 0 :(得分:0)

您转到IIS池,打开高级属性。在那里,您可能会看到一些默认身份被选作池的身份。在那里,您需要设置一个身份,该身份具有访问文件系统以及相应的注册表权限的权限。我通常会创建一个具有管理员权限和永久密码的本地用户,然后使用该本地管理员作为IIS应用程序的标识。这样我就不用担心应用程序是否需要写文件等