Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Security permissions problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johnny Jörgensen - 28 Jan 2008 10:44 GMT
I've got a program where I call a method in an internal class to update the
registry. Something like this:

Program:

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

namespace MyNameSpace
{
   public class Program
   {
       [STAThread]
       static void Main(string[] args)
       {
            RegistryClass myClass = new RegistryClass();
            myClass.UpdateRegistry();
       }
   }
}

RegistryClass:

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

namespace MyNameSpace
{
   internal sealed class RegistryClass
   {
       internal void UpdateRegistry()
       {
              //Update HKEY_CURRENT_USER
       }
   }
}

I get an error when trying to update the registry saying that I don't have
sufficient permission. So I trird doing like the documentation to the
RegistryKey class says
(http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx)
and changed the program code to:

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

namespace MyNameSpace
{
   [assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
ViewAndModify = "HKEY_CURRENT_USER")]
   public class Program
   {
       [STAThread]
       static void Main(string[] args)
       {
            RegistryClass myClass = new RegistryClass();
            myClass.UpdateRegistry();
       }
   }
}

It doesn't work, however. VS complains that "assembly is not a valid
attribute location for this declaration".

Where do I put it then? I think I've tried all possible locations, but to no
avail.

Can anybody tell me what I'm doing wrong? I've done a lot of programs before
that modifies the registry, and this is the first time I've run into a
security problem, so I really don't know why it happens now when it hasn't
happened before....

Cheers,
Johnny J.
Jon Skeet [C# MVP] - 28 Jan 2008 10:59 GMT
> I've got a program where I call a method in an internal class to update the
> registry. Something like this:

<snip>

> It doesn't work, however. VS complains that "assembly is not a valid
> attribute location for this declaration".
>
> Where do I put it then? I think I've tried all possible locations, but to no
> avail.

Put it outside the namespace declaration.

> Can anybody tell me what I'm doing wrong? I've done a lot of programs before
> that modifies the registry, and this is the first time I've run into a
> security problem, so I really don't know why it happens now when it hasn't
> happened before....

Are you running this program across the network by any chance? That's
the normal source of unexpected security issues.

Jon
Johnny Jörgensen - 28 Jan 2008 11:16 GMT
Stupid me. I just did:

RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"bla\bla\bla");
and not:

RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"bla\bla\bla", true);

Which makes the key readonly...

Problem solved, case closed - sorry for that...

Cheers,

/Johnny J.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.