Sharing .net library DLLs between Asp classic and Asp.net

June 11th, 2009 6 comments

Preface

Let me first start by saying that I am writing this example after I spent many hours frustrated at the lack of documentation, the abundance of disinformation, and the endless chasing of MSDN dead links. The information contained here is based off many sources all over the net. Though I did not find a single one that clearly laid out how to do it all at once. I will attempt to do that here, in a clear manner; these are the steps that I took to get consistent working results. There may be steps here that are not needed, though I have worked to remove as many of them as time would allow some may have slipped by.

My examples and screen shots will be from
Visual Studio 2005, Standard Edition, Frame work ver. 2.0.5xxx sp2
Web server this example was deployed on was
Windows 2003 Standard running IIS 6.0 / ASP 3.0


The first thing you are going to need to get this project off the ground is a Library DLL. There are a few changes you need to be aware before writing your Library DLL.

Methods should not be static

I am guessing here, due to the fact that asp classic does not support static classes, it stands to reason, it can’t reference one in a DLL library. I attempted a few different was to get this to work and constantly received the following error
Microsoft VBScript runtime error ’800a01b6′ Object doesn’t support this property or method: ‘myMethod’.”
My advice, save the headache and avoid the static methods, if you are reading this, and know a solution please comment.


Dll must be marked as Com-visible

To make a DLL com-visible, you need to modify the assembly information. There are multiple ways of doing this, the easiest method is to use visual studio, go to your projects properties then click the “Assembly Information” button and check the checkbox at the bottom of the window that says “Make assembly COM-Visible” (as seen below)
com-visible
you can also do this by hand by manually editing the AssemblyInfo.cs and adding the following code

[assembly: ComVisible(true)]

Lastly, you can make these changes at run time. I don’t suggest doing that, but if you must, googling “Com-visible runtime” will likely bring you to the right place for it, (I’d post a link to the MSDN page, but in my experience it will change in the future and ultimately be useless.) Another important note, you should not confuse this with, “Register for COM interop” in the build section of the projects properties. In fact you need to make sure this option is UNCHECKED as it will cause problems later, I’ll explain more on this in the following steps.


Your Project Should be Signed

Long story short this step, is done mainly to avoid warnings and possible issues of entering your assembly into the GAC (Global Assembly Cache). This step is easily done using the Visual studio IDE, and Googling “signing assemblies” will show many tutorials on how to do this from the command line if you wish to do so. Signing an Assembly will also protect your libaray from being confused or replaced by other assemblies by mistake, or through nefarious means.
signingbuild


Building the Installer

Now that you have got the prep work done and your code written, you will need to get your library on to your web server, once again this can be done by hand through various command line methods. But the easiest and most convent way for the developer, and web server management is to do this via a setup project, using the installer will add an easy way to add and remove your library to the server. To do this you need to create a new setup project.

newproject

Once you have your setup project added to your library solution,

  1. Click on the setup project in the solution explorer window, you should now see a new set of icons for this type of project
  2. The second icon is for the “File System Editor” click this and it will bring up a new window for “File System on Target Machine
  3. Right click in the left hand side of the window and select “Add Special Folder” select “Global Assembly Cache Folder.”
  4. Single Click the “Global Assembly Cache Folder.” once highlighted, right click on it and select “Add, the select “Product output
  5. In the popup window select “Primary Output

See screen shot below

setup
At this point, you should be ready to build the project and produce your installer, the setup MSI and setup.exe should be in the setup projects Release or Debug folder depending on the option you chose during build time.

Note:
If you are have a .tlb file being placed in your CAG folder along with your primary output, this is a result of having “Register for COM interop” checked in your coding projects build options, to resolve this, either uncheck the box in the build options, or add a “ExcludeFilter” for *.tlb in your installer project.


Installing

Copy the Msi and setup.exe over to your web server and install your Library, this should be done from an administrator account. Assuming all went well in your install process you should check the GAC,Typically (c:\windows\assembly) to make sure your library is registered. while here you will need to gather a bit of information so that we can reference the library in our asp.net website. you are going to need the
Name:
Culture:
Version:
Public Key Token:

You can do this by right clicking on your assembly and going to properties.


Using the Code

Once you have this information, you are ready to start using your library on your site, first we will cover using the code in asp.net(c#). The first thing we need to do, is to add the assembly to your web.config for your site. To do this, add the following code to your web.config
(you shouldn’t need to add the system.web its there to show placement)

<system.web>
  <compilation>
    <assemblies>
      <add assembly="MyAssembly, Version=1.0.0.1, Culture=neutral,PublicKeyToken=8c7fff0114cec090" />
    </assemblies>
  </compilation>
</system.web>

Once you have the assembly information added to your web.config, you can use the assembly like any other referenced library. Here is an Example

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     MyAssembly.Crypto tmpExample = new MyAssembly.Crypto();

     string phrase = "test";
     string crypted = tmpExample.Encrypting(phrase);
     string decrypted = tmpExample.Decrypting(crypted);

     Response.Write("Test phrase " + phrase+"<br>");
     Response.Write("encrypted " + crypted + "<br>");
     Response.Write("decypted " + decrypted + "<br>");
    }
}

Since you have made the Library available system wide, and made it com-visible, you won’t have to do
anything to make it visible to asp classic, other than adding it to the code itself. Here is an example of how to use the Library in asp classic (vbscript)

Dim tmpExample
Set tmpExample = Server.CreateObject("MyAssembly.Crypto")

phrase = "test"
crypted = tmpExample.Encrypting(phrase)
decrypted = tmpExample.Decrypting(crypted)

Response.Write("Test phrase " + phrase+"<br>")
Response.Write("encrypted " + crypted + "<br>")
Response.Write("decypted " + decrypted + "<br>")


Conclusion

I have tried to be as detailed as possible without being to verbose, but it is possible some details may have slipped past me. So if I have made a mistake, feel free to leave a comment, and I will work hard to resolve the issue. Also, I have found other partial examples on the net that suggest doing the same thing, but in different ways. Some of these examples turned out to be either unnecessary, or completely wrong. What I have posted here is the a method I have found to be the fastest, easiest, and most reliable way of sharing the library DLL between asp.net and asp classic via COM.

Captcha without graphics libraries

May 23rd, 2009 1 comment

Sometimes we need to perform Captcha on a form, but we don’t have access to a method of dynamically generating graphics, you can if you wish to bring in a third party and use something like reCaptcha. But if you aren’t keen on the third party idea, than this is an alternative.

Another possible use for this method is context based captcha, so say for example you presented your user with the following picture

What show am I on?

What show am I on?


Since there are multiple answers, you can add multiple entries into your has hash

<?php
$cc_hashtable["star trek"] = "img/sjirlzqoaopd.jpg";
$cc_hashtable["startrek"] = "img/sjirlzqoaopd.jpg";
$cc_hashtable["star trek: TOS"] = "img/sjirlzqoaopd.jpg";
$cc_hashtable["old star trek"] = "img/sjirlzqoaopd.jpg";
...
?>

There are limitations to this method, such as language issues, for example, due to my poor choice in question construction if someone from German came across my form they may enter “Raumschiff Enterprise” which is correct to them, but would not work for your site. So be aware of that, it is best to come up with single answer questions, and localization may require a hash for each language.

With all that said, the first thing you will need to do is to gain access to images that contain captcha data, the easiest way to do this, is to find a site that can dynamically generate captcha images and copy and image from there, and refresh and repeat till you have built up enough images to meet your needs, the more images, the more the system will seem dynamic, and decrease the chances of your method being found out. Once you have your images, you want to add them to the cc_hash.php, you also want to load the captcha phrase from each image as its key in the hash table.

The second thing is to integrate the code into your form, the functions are set up in such away placing the following function calls in your form will generate two new fields in your form variables
adds the captcha image to the form.

<?=cc_form_image($cc_hashtable)?>

adds the input field for the captcha to the form

<?=cc_form_text_field()?>

try it out for yourself
Cheap Check Example

continue reading for the code examples.
Read more…

Progress

May 20th, 2009 No comments

The process of moving from a (failed) google apps attempt at a personal site is almost complete, the software section has been brought up to date with my desktop applications, and in the coming days I hope to add a few scripts I’ve written to this journal section.