Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 4th, 2004, 02:48 AM
Authorized User
 
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default registry permission problems

I want to alow currently login users to have write permission to HKEY LOCAL MACHINE registry path (by default this user have only read permision). I using C# .NET 2003.
My code is:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using Microsoft.Win32;
using System.Security.Permissions;
using System.Security;

....

[RegistryPermissionAttribute(SecurityAction.Assert, Create = "HKEY_LOCAL_MACHINE\\SOFTWARE\\TEST")]
[RegistryPermissionAttribute(SecurityAction.Assert, All = "HKEY_LOCAL_MACHINE\\SOFTWARE\\TEST\\data")]
private void btnAdd_Click(object sender, System.EventArgs e)
{
RegistryPermission f = new RegistryPermissio(RegistryPermissionAccess.AllAcce ss,"HKEY_LOCAL_MACHINE\\SOFTWARE");
f.AddPathLis(RegistryPermissionAccess.AllAccess,"H KEY_LOCAL_MACHINE\\SOFTWARE\\TEST");
    RegistryKey LM=Registry.LocalMachine;
    RegistryKey SW=LM.OpenSubKey("Software");
    RegistryKey mykey=SW.OpenSubKey("TEST",true);//On this point I get exception !!!!
    mykey.CreateSubKey("mama");
    mykey.SetValue("data","10");
}

 
Old July 5th, 2004, 03:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

I dont think your problems lies with permission code. The permission granting code is alright. The exeption you are getting is perhaps due to the absense of TEST subkey in the Registery.

Nowhere in the code you have any backup for the case where TEST key is missing. Either add code to create TEST subkey first in case it is missing or manually add this key to the registry to get the code running.

Ankur
 
Old July 5th, 2004, 03:55 AM
Authorized User
 
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry Ankur but I manually add this key to registry and all I want to change its value. But dont work?
Are you shure that I dont have any error in my code?


 
Old July 22nd, 2004, 03:51 AM
Registered User
 
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to keenweng Send a message via MSN to keenweng
Default

I want to access to a remote computer registry using the code below but i get an error: error : An unhandled exception of type "System.UnauthorizedAccessException" occured in system.management.dll, Additional information : Access is denied . Remote access services in both of the pc is enable and both are using WinXP.

using System;
using System.IO;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Security;

[assembly: RegistryPermissionAttribute(SecurityAction.Request Minimum,
    Read = @"HKEY_CURRENT_USER\Environment")]
[assembly: SecurityPermissionAttribute(SecurityAction.Request Minimum,
    UnmanagedCode = true)]

class RemoteKey
{
static void Main(string[] args)
{
RegistryKey environmentKey;
string remoteName;

        // Check that an argument was specified when the
        // program was invoked.
        if(args.Length == 0)
        {
    Console.WriteLine("Error: The name of the remote " +
    "computer must be specified when the program is "
         + "invoked.");
    return;
        }
        else
        {
            remoteName = args[0];
        }

        try
        {
        // Open HKEY_CURRENT_USER\Environment
        // on a remote computer.
environmentKey = RegistryKey.OpenRemoteBaseKey(
RegistryHive.CurrentUser,remoteName).OpenSubKey("E nvironment");
        }
        catch(IOException e)
        {
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
            return;
        }
Console.WriteLine("\nThere are {0} values for {1}.",
environmentKey.ValueCount.ToString(), environmentKey.Name);
    foreach(string valueName in environmentKey.GetValueNames())
        {
    Console.WriteLine("{0,-20}: {1}", valueName,
    environmentKey.GetValue(valueName).ToString());
        }

        // Close the registry key.
        environmentKey.Close();
    }
}
 
Old July 26th, 2004, 07:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Hi Laslo, First of all accept my apologies for leaving the discussion in between.

About the code : as I said the code has no problem but it wont do what you want it to do, that is assign your application the permissions. What I meant to say is that these attributes are used to first QUERY what permissions are available to you in the zone where your app resides and then ASSOCIATE the selected permissions out of available permissions in the zone to your code.

You can choose only from the permissions available to you.

In your case when your application tries to access the remote computer’s registry your code is in LocalIntranet zone and by default no code in the LocalIntranet zone is allowed to access critical resources like registry.

You code is right but you need to make your remote machines entrust the code residing in LocalIntranet zone, if that’s fissile for you, because that requires running Policy Administration tools like caspol.exe on the remote machines and that opens the doors for just about anything in that zone which is not recommended.

That’s all I have on the topic for now but I’m sure there must be a better way of getting it done. Do update the forum if you find anything new in this regard.

Ankur
 
Old July 26th, 2004, 09:09 AM
Authorized User
 
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your time Ankur.In the meantime I solve this problem (the problem was to remember http adress of remothing machine on Local machine) using config files in exe directories.
Now I'm almost sure that this kind of problem can't solve by giving certain perrmisions to cureent user under local machine path in Registry Tree.
Any way to config file in solve this prety well.
Thanks again and ewery luck :-)



 
Old May 31st, 2006, 06:22 AM
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi

I want to alow currently login users to have write permission to HKEY LOCAL MACHINE registry path (by default this user have only read permision).

please guide me how to achieve it?

Thanking u
Parashuram osekar

 
Old June 7th, 2007, 05:39 AM
Authorized User
 
Join Date: Apr 2005
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi keenweng .,, i too have same problem un authorisation access it shows warning about that assembly: in code the warning is

  'assembly' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'type'. All attributes in this block will be ignored.



        If u got a solution , please letme know..... its urgent for me
  mail id sangram82@gmail.com

 
Old June 15th, 2007, 01:35 AM
Registered User
 
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I also got this problem and moved this code outside of my namespace.I think this error may be due to the declaration of the code inside your namespace.just try this.

Cut your code and paste it next to the using declaration statements.I think this will resolve your problem



Quote:
quote:Originally posted by jilly
 hi keenweng .,, i too have same problem un authorisation access it shows warning about that assembly: in code the warning is

'assembly' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'type'. All attributes in this block will be ignored.



        If u got a solution , please letme know..... its urgent for me
mail id sangram82@gmail.com

 
Old June 18th, 2007, 05:22 AM
Authorized User
 
Join Date: Apr 2005
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx , now it didnt give any error, when in running it gives

" Requested registry access not allowed"

i have to read HKEY_LOCAL_MACHINE\Software\\Microsoft\\Windows\\C urrentVersion\\Uninstall

all the subkeys under this key to list the installed applications on that system.
can anyone suggest me a solution.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Permission Problems on Deployment Hank_Marietta BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 August 22nd, 2007 12:29 PM
(0x800A0046) Permission denied,not IIS permission! David Hay Classic ASP Basics 3 April 30th, 2006 10:47 PM
Registry snowy0 VB.NET 2002/2003 Basics 1 September 8th, 2004 10:40 AM
Registry jake Pro VB.NET 2002/2003 5 November 6th, 2003 04:02 AM
Permission problems ?? johnly SQL Server DTS 0 July 7th, 2003 05:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.