Using SmartAssembly with an ASP.NET website
Published 03 January 2013
ASP.NET websites are dynamically compiled on the server as needed but SmartAssembly can only protect precompiled .NET assemblies. This makes it unclear how to protect a website with SmartAssembly.
The solution is to precompile the website. The .NET Framework ships with a utility for this purpose called aspnet_compiler.exe. This will create a series of DLLs from your website code placeholders, so the original code is no longer available in the website folder. Once the compilation is complete, you protect the resulting DLLs in the bin folder using SmartAssembly.
If you're also implementing error reporting, you must protect all dlls separately, as merging and embedding may break the error reporting function.
To implement SmartAssembly protection and error reporting in ASP.NET:
- Add a reference to %ProgramFiles%\Red Gate\Smartassembly 8\SDK\bin\SmartAssembly.ReportException.dll in the web project.
- Open the codebehind file for Global.asax (Global.aspx.cs) in Notepad or Visual Studio.
If the method protected void Application_Error(Object sender, EventArgs e) does not exist, create it.
The method should contain these lines:(C#)
Exception exc = Server.GetLastError().GetBaseException(); SmartAssembly.ReportException.ExceptionReporting.Report(exc);
(VB)
Dim exc As Exception = Server.GetLastError().GetBaseException() SmartAssembly.ReportException.ExceptionReporting.Report(exc)
- Save Global.asax.cs (or vb)
Use Aspnet_Compiler to create pre-compiled code for the website:
aspnet_compiler -v "/" -d -p "c:\mywebsite" "c:\mynewwebsite"
- Open the dependent DLLs (not App_Web_xxx.dll) and create a SmartAssembly project for each.
- If you are using Automatic Error Reporting, set it to report errors silently.
- Build the DLL in a new folder in a bin subfolder (for example, C:\mynewSAwebsite\bin).
- Open the main DLL (App_Web_xxx.dll) in a new SmartAssembly project.
- Set up error reporting as for the dependent DLLs, but do not merge or embed the dependencies.
Copy all of the files that are not DLLs from the original compiled website to the one you just created:
xcopy c:\mynewwebsite c:\mynewSAwebsite /E /EXCLUDE:\bin\
The compiled and protected website is now available in the c:\mynewSAwebsite folder. Configure this folder as an IIS web application to make it available to users of your website.