Wednesday, July 16, 2008

Run .NET Code from a shared drive/directory

Security is an issue whenever running code from an untrusted source like a shared or network drive. It would be nice to be able to put an application on a share drive to run whether it be in C# or VB .NET.

Normally, when you try to run .NET code on a shared network drive you will get an exception like System.Security.Permissions.SecurityPermission .


There is a way to relax the security via the caspol.exe command(For .Net Framework 2.0 "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -s off").

An application may be launched with one simple batch file as follows:

AllInOne.BAT:
@ECHO OFF

REM Relax security constraints for .NET
START C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -s off

REM Pause until first command has executed
Set Count=1
:LOOP
Set /A Count=%Count%+1
If %Count% lss 100 GoTo :LOOP

REM Launch .NET application
"""SomeExecutable.exe"""

REM Remove relaxation of security constraints
TASKKILL /im CasPol.exe

@ECHO ON

*****************************

A pause is required to allow caspol to start up before launching the .NET application.

But of course this implementation does meet the requirements of launching the .NET application from the shared location but there are those DOS windows which are open and probably not that desirable.

These can be avoided with 2 BAT files and 2 VBS files as follows. This is just one way of doing it but there are probably better ways out there.

launch.bat (Main file)
****************
@ECHO OFF
REM Main Launch for .NET application on share drive
REM VBS script used so there is not a DOS console opened up

START launchtasks.vbs

@ECHO ON

****************

launchtasks.vbs
****************
On Error Resume Next

CmdLine = """tasks.bat"""
Params = ""
CmdLine = CmdLine & " " & Params

'Launch without window
wscript.echo fShellRun(CmdLine)

Function fShellRun(sCommandStringToExecute)
' This function will accept a string as a DOS command to execute.
' It will then execute the command in a shell, and capture the output into a file.
' That file is then read in and its contents are returned as the value the function returns.
Dim oShellObject, oFileSystemObject, sShellRndTmpFile
Dim oShellOutputFileToRead, iErr
Set oShellObject = CreateObject("Wscript.Shell")
Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
On Error Resume Next
oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
iErr = Err.Number
On Error GoTo 0
If iErr <> 0 Then
fShellRun = ""
Exit Function
End If
fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile,1).ReadAll
oFileSystemObject.DeleteFile sShellRndTmpFile, True
End Function

****************

tasks.bat
****************
@ECHO OFF

REM Relax security constraints for .NET
REM Run this in VBS to avoid a DOS WINDOW
START caspol.vbs

REM Pause until first command has executed
Set Count=1
:LOOP
Set /A Count=%Count%+1
If %Count% lss 70 GoTo :LOOP

REM Launch .NET application
"""SomeExecutable.exe"""

REM Remove relaxation of security constraints (The >NUL is to avoid showing the info on the processes terminated)
TASKKILL /im CasPol.exe >NUL

@ECHO OFF

****************


caspol.vbs
****************
On Error Resume Next

'The command below will also depend on the .NET version of the application you are running

CmdLine = """C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CasPol.exe"""
Params = "-s off"
CmdLine = CmdLine & " " & Params

'New way without window
wscript.echo fShellRun(CmdLine)

Function fShellRun(sCommandStringToExecute)
' This function will accept a string as a DOS command to execute.
' It will then execute the command in a shell, and capture the output into a file.
' That file is then read in and its contents are returned as the value the function returns.
Dim oShellObject, oFileSystemObject, sShellRndTmpFile
Dim oShellOutputFileToRead, iErr
Set oShellObject = CreateObject("Wscript.Shell")
Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
On Error Resume Next
oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
iErr = Err.Number
On Error GoTo 0
If iErr <> 0 Then
fShellRun = ""
Exit Function
End If
fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile,1).ReadAll
oFileSystemObject.DeleteFile sShellRndTmpFile, True
End Function

****************

17 comments:

Brad Abrams said...

Have you checked out .NET Framework 3.5 SP1? This feature is addressed in that release.
You can actually run .NET code from a network share without needing the fancy script file magic.
http://blogs.msdn.com/brada/archive/2008/05/05/visual-studio-2008-and-net-framework-3-5-sp1-beta.aspx

vishal said...

GOT BORED BY SEARCHING FOR PHONE NUMBERS IN TELEPHONE DIRECTORY, NOW U DON’T WORRY FOR SEARCHING,WE MAKE YOUR JOB EASY FOR SEARCHING PHONE NUMBERS click here AND LOGIN INTO OUR SITE AND ENJOY THE SEARCH
THANK YOU

Anonymous said...

I am thinking about performing a reverse cell look up by going online. I am having these phone calls by someone that I don't know and am curious who believe that they are contacting. Lots of different crazy texts and messages are being left on my personal voice mail and its starting to drive me insane. So, where should i locate these reverse phone search pages?

Anonymous said...

Greetings! I know this is somewhat off topic but I was wondering
if you knew where I could find a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm
having difficulty finding one? Thanks a lot!

Feel free to surf to my blog post :: Trans Vaginal Settlement Funding

Anonymous said...

I comment when I especially enjoy a article on a site or if
I have something to valuable to contribute to the conversation.
Usually it is triggered by the sincerness displayed in the article I read.
And on this post "Run .NET Code from a shared drive/directory".

I was actually excited enough to leave a comment :
-P I actually do have a couple of questions
for you if you don't mind. Could it be just me or do a few of these comments look like coming from brain dead
individuals? :-P And, if you are posting on other online social
sites, I'd like to follow anything new you have to post. Would
you list all of all your community pages like your
Facebook page, twitter feed, or linkedin profile?|

Look into my web site; pity 2014 program

Anonymous said...

It is a great time to be spontaneous and maybe
even a little silly. If you are still unsure of the photographer,
you can always seek referrals by asking friends or family members about their experiences with photographers used in their weddings.
If you are developing your client's pictures yourself, Photoshop is an excellent
photo editing program.

My webpage ... wedding photography ky ()

Anonymous said...

Awesome site you have here but I was curious if you knew of any forums that cover the same topics discussed here?
I'd really love to be a part of online community where I can get responses from other
experienced people that share the same interest. If you have
any suggestions, please let me know. Thanks a
lot!

Also visit my page cenaless

Anonymous said...

Hey there! I know this is kind of off topic but I was wondering iff you knew where I could
locate a captcha plugin for my comment form? I'm using the same
blog pkatform as yours and I'm having trouble findiing
one? Thanks a lot!

My website; SEO company prices

Anonymous said...

Hi there! I could have sworn I've visited this web site before but after looking at some of the posts I realized it's new to me.
Anyhow, I'm definitely pleased I came across it and I'll be book-marking it and checking back
regularly!

Feel free to visit my blog post :: premium garcinia extract review (http://www.subconscious-imagery.com/2014/04/verify-line-buy-garcinia-cambogia-extract)

Anonymous said...

Greetings from Idaho! I'm bored to tears at work so I decided
to browse your site on my iphone during lunch break.
I enjoy the knowledge you present here and
can't wait to take a look when I get home. I'm amazed
at how fast your blog loaded on my phone .. I'm not even using
WIFI, just 3G .. Anyways, great blog!

Here is my site - Omega Scarf

Anonymous said...

Kate Spade Outlet Celine Outlet
Chanel Handbags Marc Jacobs Handbags Chanel Outlet Marc Jacobs Handbags Outlet Oakley Sunglasses Wholesale Marc Jacobs Outlet Chanel Outlet chanel handbags Outlet Christian Louboutin Outlet Online
Prada Outlet Nike Air Max Lebron James Shoes Hermes Birkin Christian Louboutin Outlet Chanel Handbags Hermes Birkin chi flat iron website Christian Louboutin Outlet Online Giuseppe Zanotti Sneakers On Sale Lebron James Shoes Lululemon Athletica Oakley Sunglasses Outlet Christian Louboutin Shoes chi Flat iron website Gucci Handbags
Nike Free Celine Outlet Gucci Handbags Outlet

Anonymous said...

Cons: Lowest potency, couldn't discover a business website, need
to take 3 capsules each day to get even 1500mg
Nature's Symbiotics Garcinia Cambogia HP - Natural Appetite Suppressant
and Premium Belly Fat Burner - Pure 1600mg Extract, 60% HCA, 30 Day Supply.

Bitter orange extract chemicals may increase heart
rate, blood pressure, migraines and cause fainting,
strokes and heart attacks. First, transdermal diet patches never have been approved by these US Food and
Treatment Administration for weight diminishment or weight control.


my web page reviews of pure garcinia cambogia

Anonymous said...

Good day! I know this is somewhat off topic but I was wondering if you
knew where I could get a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having
trouble finding one? Thanks a lot!

my web site - bmw repair st george ut

Anonymous said...

I really like your blog.. very nice colors & theme.
Did you create this website yourself or did you hire someone to do it for
you? Plz reply as I'm looking to design my own blog and would like
to know where u got this from. thanks

Feel free to visit my web page: Very hairy women pics

Anonymous said...

in generating new performing as grave as you tramp finished your day.
If you grape juice represent it a situation to get a
adhesive disorderliness. Neutrals are big all temper. This period
of time, the creamier neutrals are specially nonclassical.

Try search for payout from a web distributer. If organism Cheap Ray Ban Sunglasses Oakley Sunglasses Outlet Ray Ban Sunglasses Cheap Oakley Sunglasses Oakley Sunglasses Outlet Oakley Sunglasses Cheap Ray Ban Sunglasses Cheap Oakley Sunglasses cheap ray ban sunglasses Oakley Sunglasses Outlet Cheap Oakley Sunglasses Oakley Sunglasses Wholesale Cheap Oakley sunglasses Cheap Oakley Sunglasses
Cheap Oakley Sunglasses
Oakley Sunglasses Outlet
Ray Ban Sunglasses Ray Ban Sunglasses
Cheap Ray Ban Sunglasses Oakley Sunglasses Outlet Oakley Sunglasses Cheap Ray Ban Sunglasses Oakley Sunglasses Wholesale path, you volition physique squad fiber.

ever observe a log of every folio on your electronic mail to be confident you can't let
down with a dig, or in a Brobdingnagian response this period.
You don't impoverishment to miss at a measure.
A cracking way to physical process the amount of

Take a look at my weblog; Cheap Ray Ban Sunglasses

Anonymous said...

If you would like to get a good deal from this paragraph then you have to apply these methods to your won website.


Also visit my homepage ... Secret Money System

Anonymous said...

Hi colleagues, how is the whole thing, and what you want to
say regarding this article, in my view its genuinely
remarkable in support of me.

Also visit my web site ... home inspector in staten island