简体   繁体   中英

How to enable ASP.NET write access to the registry key 'HKEY_CURRENT_USER\Software\myAppKey\subdir'?

In my ASP.NET web app My.Computer.Registry.SetValue(Key_Name, Value_Name, Metric) causes "access to registry key is denied" error.
App is running on Azure Windows Server 2012 VM. However, my .NET windows app also running on same VM, writes to registry ok.

I've tried setting regedit > the key: to everyone=full control, still denied.

My .NET windows app on same computer (Azure Windows Server 2012 R2) writes to registry key "HKEY_CURRENT_USER\\Software\\" & SOFTWARE_PROGRAM_NAME _ & "\\Metrics\\" & Key_Name without problem.

HERE IS ASP.NET CODE THAT CAUSES PROBLEM...………..

' 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

HERE IS ERROR WHEN I BROWSE MY 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

You go to your IIS pool, open advanced properties. There you probably see some default identity selected as identity for your pool. There you need to set an identity that has permissions to access file system and correspondingly, the registry. I usually create a local user with admin rights and permanent password, and then use that local admin as identity for my IIS apps. This way I don't worry if app needs to write a file, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM