Home All Groups Group Topic Archive Search About

'System.Security.SecurityException' when running .exe file from ot

Author
12 Aug 2005 12:32 PM
Svein Erik
I've developed a small console application that gathers systeminformation
about the machine. I can run this from a shared network drive, and it works
fine. But when i implement 2 more functions that writes a key to the registry
and creates a desktop icon on the machine that runs the app, i get a
System.Security.SecurityException.
Any tips on how i can avoid this?
Here is the code that generates the error (i've found out that both the
registry-writing & the desktop creation generates the same error):

            //Create registry entry
            RegistryKey hku = Registry.Users;
            RegistryKey numbers =
hku.OpenSubKey("S-1-5-21-985536280-989403192-929701000-1870");
            RegistryKey software2 = numbers.OpenSubKey("Software");
            RegistryKey microsoft2 = software2.OpenSubKey("Microsoft");
            RegistryKey windows2 = microsoft2.OpenSubKey("Windows");
            RegistryKey currVer = windows2.OpenSubKey("CurrentVersion");
            RegistryKey internetSettings = currVer.OpenSubKey("Internet
Settings",true);

            string regValue =
internetSettings.GetValue("ProxyOverride").ToString();
            int exists = regValue.IndexOf(@"http://192.168.60.52");
            //Om den ikkje finnes, legg den til i registeret
            if( exists == -1 )
            {
                regValue += @";http://192.168.60.52";  
                internetSettings.SetValue("ProxyOverride", regValue);
                internetSettings.Close();
            }
            else
            {
                internetSettings.Close();
            }

            //Create desktop shortcut:   

            IWshRuntimeLibrary.IWshShell_Class TheShell = new 
IWshRuntimeLibrary.IWshShell_Class();
            string desktopDir =
System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            IWshRuntimeLibrary.IWshShortcut_Class shellLink =
(IWshRuntimeLibrary.IWshShortcut_Class)TheShell.CreateShortcut(desktopDir +
@"\SYS-IKT - Helpdesk.lnk");
            shellLink.TargetPath = @"http://192.168.60.52/helpdesk/";
            shellLink.Save();          


        }

Author
13 Aug 2005 11:10 AM
Nicole Calinoiu
See http://blogs.msdn.com/shawnfa/archive/2003/06/20/57023.aspx for an
explanation of why this happens, as well as instructions for granting your
assembly the additional CAS permissions it needs.


Show quoteHide quote
"Svein Erik" <Svein E***@discussions.microsoft.com> wrote in message
news:84274DCE-B92E-447B-8ACB-F015FA821A7B@microsoft.com...
> I've developed a small console application that gathers systeminformation
> about the machine. I can run this from a shared network drive, and it
> works
> fine. But when i implement 2 more functions that writes a key to the
> registry
> and creates a desktop icon on the machine that runs the app, i get a
> System.Security.SecurityException.
> Any tips on how i can avoid this?
> Here is the code that generates the error (i've found out that both the
> registry-writing & the desktop creation generates the same error):
>
>            //Create registry entry
>            RegistryKey hku = Registry.Users;
>            RegistryKey numbers =
> hku.OpenSubKey("S-1-5-21-985536280-989403192-929701000-1870");
>            RegistryKey software2 = numbers.OpenSubKey("Software");
>            RegistryKey microsoft2 = software2.OpenSubKey("Microsoft");
>            RegistryKey windows2 = microsoft2.OpenSubKey("Windows");
>            RegistryKey currVer = windows2.OpenSubKey("CurrentVersion");
>            RegistryKey internetSettings = currVer.OpenSubKey("Internet
> Settings",true);
>
>            string regValue =
> internetSettings.GetValue("ProxyOverride").ToString();
>            int exists = regValue.IndexOf(@"http://192.168.60.52");
>            //Om den ikkje finnes, legg den til i registeret
>            if( exists == -1 )
>            {
>                regValue += @";http://192.168.60.52";
>                internetSettings.SetValue("ProxyOverride", regValue);
>                internetSettings.Close();
>            }
>            else
>            {
>                internetSettings.Close();
>            }
>
>            //Create desktop shortcut:
>
>            IWshRuntimeLibrary.IWshShell_Class TheShell = new
> IWshRuntimeLibrary.IWshShell_Class();
>            string desktopDir =
> System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
>
>            IWshRuntimeLibrary.IWshShortcut_Class shellLink =
> (IWshRuntimeLibrary.IWshShortcut_Class)TheShell.CreateShortcut(desktopDir
> +
> @"\SYS-IKT - Helpdesk.lnk");
>            shellLink.TargetPath = @"http://192.168.60.52/helpdesk/";
>            shellLink.Save();
>
>
>        }
>
>
Author
16 Aug 2005 6:31 AM
Svein Erik
"Nicole Calinoiu" wrote:

> See http://blogs.msdn.com/shawnfa/archive/2003/06/20/57023.aspx for an
> explanation of why this happens, as well as instructions for granting your
> assembly the additional CAS permissions it needs.

Thanks, i have infact read this article, but this program won't  run on any
other computer..! The computers i've tried it on has Administrator rights,
but it throws the exception anyway.
I just made a script that xcopy the program from a network disk to the
clients c:\ and runs it from there, but the exception is thrown and i can't
figure out why.
Author
16 Aug 2005 11:29 AM
Nicole Calinoiu
Show quote Hide quote
"Svein Erik" <SveinE***@discussions.microsoft.com> wrote in message
news:C53C1B90-3FD7-4BE0-9896-79F0D2730CAF@microsoft.com...
>
>
> "Nicole Calinoiu" wrote:
>
>> See http://blogs.msdn.com/shawnfa/archive/2003/06/20/57023.aspx for an
>> explanation of why this happens, as well as instructions for granting
>> your
>> assembly the additional CAS permissions it needs.
>
> Thanks, i have infact read this article, but this program won't  run on
> any
> other computer..!

Did you apply the CAS policy changes to every computer on which you want the
application to run?  (The changes must be applied to each client computer,
not the server on which the assemblies happen to be stored.)


> The computers i've tried it on has Administrator rights,
> but it throws the exception anyway.

User permissions do not override CAS permissions (or vice versa).  If CAS
permissions are limiting your application's activities, you must address
this regardless of the Windows permissions granted to the user running the
applicaiton.


> I just made a script that xcopy the program from a network disk to the
> clients c:\ and runs it from there, but the exception is thrown and i
> can't
> figure out why.

Does this mean that the exception is still thrown when the assembly is run
from the local c:\ drive?  If so, could you please specify exactly how you
are launching the application?  If not, could you please explain exactly
under what conditions the exception is thrown or not thrown?