Last two days I have been struggling with deploying a VSTO 2005 solution to another location (i.e. another directory other than the bin output directory within my VSTO project) to research how VSTO solutions security behaves if you move them.
I knew that in order to allow your solutions to run in a specific location you had to grant the solution some trust before allowing you to run the code. One way is using the .NET configuration tools, the other is using caspol.
Well I apparently did some bad things with these tools and ended up removing my Visual Studio 2005 and reinstalling because none of my solutions worked after I messed up security settings.
When I reinstalled everything, and was up and running again I tried to do the exercise again, now more controlled:
- Rebuild the VSTO 2005 solution with Visual Studio 2005 (beta1)
- Copy the output from the bin directory to another location
- Test: try to run without changing security.... code did not run, that was expected!
- Adjust security settings for the new location using caspol:
created a tiny script:
- cd %windir%\microsoft.net\framework\v2.0.40607
- caspol.exe -polchgprompt off
- caspol.exe -user -addgroup 1 -allcode Nothing -name "MyTestCodeGroup"
- caspol.exe -user -addgroup MyTestCodeGroup -url "file://C:/MyNewLocation/*" FullTrust -name "MyTestName"
- caspol.exe -polchgprompt on
- pause
(note: I prefixed the lines here with "- " te show you what is on one single line as the blog is breaking the lines)
- Test: try to run after changing security.... code did run!
Hmm that was easy! What could be done in 5 minutes I happened to do in 2 days... duh!
I hope you can learn something from this so you don't have to spend a lot of time doing wrong what I did wrong...
Remember that in this example I changed the security for the complete directory. It is possible to only trust the single DLL so don't worry about someone else putting some evilhack.dll's in that directory. All you need to do is change the script to only trust your own DLL.