Thursday, April 16, 2009

Forcing pages to use SSL through ASP.Net

Save the below code (choose any one according to your applicaiton's language code) to the root of your application into a file named "ForceSSL.inc" using a text editor.

For VB.Net application
===================
<% If Request.ServerVariables("SERVER_PORT")=80 Then
Dim strSecureURL
strSecureURL = "https://"
strSecureURL = strSecureURL & Request.ServerVariables("SERVER_NAME")
strSecureURL = strSecureURL & Request.ServerVariables("URL")
Response.Redirect strSecureURL
End If %>

For C#.Net application
=====================
<% if(Request.ServerVariables["SERVER_PORT"]=="80")
{
string strSecureURL="";
strSecureURL = "https://";
strSecureURL = strSecureURL + Request.ServerVariables["SERVER_NAME"]; strSecureURL = strSecureURL + Request.ServerVariables["URL"];
Response.Redirect(strSecureURL);
}
%>

For each .aspx page that requires SSL, paste the following code at the top of the page (or under content's place holder if you are using master pages) to reference the include file from the previous step:

<!--#include virtual="ForceSSL.inc"-->

Tuesday, March 31, 2009

Load Libray(ACT.Framework.dll) Failed

ACT Library (Dlls) must reside into the Global Assembly Cache (C:\windows\assembly) to work with Visual Studio.Net.

You can not copy your dlls into this folder directly by copy/paste, rather you have to add them using gacutil.exe (resides in the C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin or C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v2.0\Bin) at command line.

e.g; C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\gacutil.exe /i c:\mydll.dll

Excel in ASP.NET

//Change the Source File's Extension to ".xml" because we are about to use OpenXML method in this function. You can even use Open() method for .txt, .csv, etc files.

//This function takes an "xml" file (even if the contents in the file are html/csv contents) and converts into a Binary Excel File.

//Idea is very simple, Open a file and Save As a binary file.

public
void ExportToExcel()

{

try

{

string xmlFile = "C:\\test.xml";

string xlsFile = "C:\\test.xls";

Microsoft.Office.Interop.Excel.Application oApp = new Microsoft.Office.Interop.Excel.Application();

System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("Excel");

System.Diagnostics.Process currentProcess = processes[processes.Length - 1];//Get the Last Excel process just created (a bit tricky)

oApp.DisplayAlerts =
false;

object obj = oApp.Workbooks.OpenXML(xmlFile, System.Reflection.Missing.Value, System.Reflection.Missing.Value);

object[] para = new object[12];

object fileName = xlsFile;

object fileFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel5;

object missing = System.Reflection.Missing.Value;

object fileMode = Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive;

para[0] = fileName;

para[1] = fileFormat;

para[2] = missing;

para[3] = missing;

para[4] = missing;

para[5] = missing;

para[6] = fileMode;

para[7] = missing;

para[8] = missing;

para[9] = missing;

para[10] = missing;

para[11] = missing;

Type MyType = (Type)(obj.GetType());MyType.GetMethod("SaveAs").Invoke(obj, para);

oApp.Workbooks.Close();

oApp.Quit();

currentProcess.Kill();

}

catch (Exception ex)

{

throw ex;

}

}