Re: DeleteSubKeyTree fails with UnauthorizedAccessException

From: Stephen McCloskey [MSFT] (stemccl_at_online.microsoft.com)
Date: 06/18/03


Date: Tue, 17 Jun 2003 15:21:21 -0700


Are you running the app from a local drive? Perhaps the application is
running in partial trust.

> RegistryPermission p = new RegistryPermission
> (RegistryPermissionAccess.AllAccess, "HKEY_CLASSES_ROOT\\CL
> SID\\");
> p.Assert();
> p.Demand();

Note that an assembly can't programmatically entice the CLR to grant it more
permission that it would otherwise receive. (It can refuse permissions,
however.) The administrator of the machine always has final control over
which permissions get granted. The programmer really doesn't (and
shouldn't) have too much control over what gets granted. So, this code
won't help things if this turns out to be a permission problem and won't
really allow you to troubleshoot.

The Demand and Assert operations are usually placed in libraries (that are
highly trusted) and are used to protect themselves from other libraries and
exes (that are not trusted.) You can't, for example, use a Demand or an
Assert within an exe to influence the permissions it will receive.

Use Demand in a library that has Permission X to require that all callers
(in different assemblies) also have permission X.

Use Assert in a Library, Foo, that has Permission X (Foo is required to have
Permission X because it calls another library, Bar ) when you do not want to
require that callers of Foo have permission X.

Stephen

"Mark Lythgoe" <regspam.10.beachvbbum@spamgourmet.com> wrote in message
news:043e01c3343d$36259d30$a101280a@phx.gbl...
> I have tried just about every combination of assembly and
> method-level RegistryPermission attributes to no avail. I
> have checked the code access level and it is
> unrestricted. My account is the local administrator on my
> machine and I can delete the registry keys manually
> without problem. The code is;
> RegistryKey CLSID = Registry.ClassesRoot.OpenSubKey
> ("CLSID");
> init();//UI stuff
> ctlStatusBar.Text = "Removing
> registry keys";//UI stuff
> this.Refresh(); //UI stuff
> foreach (int index in
> ctlItemsToDelete.CheckedIndices){
> ctlStatusBar.Text
> = "Removing " + (string)CLSID.OpenSubKey(_aItems
> [index]).GetValue("");//UI stuff
> this.Refresh();
> CLSID.DeleteSubKeyTree
> (_aItems[index]);//fails
> }
> basically it is just presenting the user with a list of
> registered COM classes to delete and then trying to delete
> them from HKEY_CLASSES_ROOT\CLSID.
>
> Anyone got any ideas?
>
> Mark
>
> PS: When I add the following above my code;
> RegistryPermission p = new RegistryPermission
> (RegistryPermissionAccess.AllAccess, "HKEY_CLASSES_ROOT\\CL
> SID\\");
> p.Assert();
> p.Demand();
> I get no errors or assertions, I have also tried
> RegistryPermission p = new RegistryPermission
> (PermissionState.Unrestricted);



Relevant Pages

  • Re: security/strong name/zones clarification needed
    ... Does the Assert code go in its ... >>this AppDomain needs to be setup before your assembly can ... >>will recieve the permission grant you expect (in this ... All my assemblies are strong named. ...
    (microsoft.public.dotnet.security)
  • Re: security/strong name/zones clarification needed
    ... control, you may have to override them and do the assert there. ... Does this zone have permission to use custom ... All my assemblies are strong named. ...
    (microsoft.public.dotnet.security)
  • Re: security/strong name/zones clarification needed
    ... Does the Assert code go in its ... >this AppDomain needs to be setup before your assembly can ... >will recieve the permission grant you expect (in this ... All my assemblies are strong named. ...
    (microsoft.public.dotnet.security)
  • Re: Code Acess Security
    ... Assert is used to prevent a stack walk. ... - My assembly is granted permission A as well as the permission to Assert ... Note that there are some major assemblies in the ...
    (microsoft.public.dotnet.security)
  • Re: CodeAccessPermission.Demand() vs Assert()
    ... > to check if a permission is granted to the current assembly, ... > will not guarantee that the protected operations will pass (unless you use ... > assert), because stack frames above might not have the permission. ...
    (microsoft.public.dotnet.security)

Loading