Monday, July 14, 2008

SSL on selective pages in MOSS

If you want to apply SSL on some selective pages on moss site. When you obtained the certificate and assigned the certificate to the site. when tried to browse the site You will got the error msg: that site has to be browsed using Https:// When try to browse the site I got page not found error.

One possible solution is to untick the Require secure channel (SSL) option in IIS, and manually forcing a SSL using HTTP Module.

public class HttpModule:IHttpModule
{
public void Dispose()
{
//dispose function required to declare for HttpModule class
}
public void Init(System.Web.HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
RedirectSSL(System.Web.HttpContext.Current.Request.Url.PathAndQuery);
}
public void RedirectSSL(string pageURL)
{
//get the collection of the pages requiring SSL and not requiring SSL
//check for the current page exists in the group of pages requiring SSL
//if page is found in ssl group then redirect using https:// otherwise redirect with http://
bool found = false;
string sslPages = System.Configuration.ConfigurationSettings.AppSettings["SSLPages"];
char[] separator;
separator = new char[] { ',' };
string[] pages;
pages= sslPages.Split(separator);
for (int i = 0; i < pages.Length; i++)
{
if (pageURL.Contains(pages[i]))
{
found = true;
break;
}
}
if (found)
{
if (System.Web.HttpContext.Current.Request.Url.Scheme.ToString() == "http")
{
string sURL = System.Web.HttpContext.Current.Request.Url.ToString();
sURL = sURL.Replace("http://", "https://");
System.Web.HttpContext.Current.Response.Redirect(sURL);
}
}
else
{
if (System.Web.HttpContext.Current.Request.Url.Scheme.ToString() == "https")
{
string sURL = System.Web.HttpContext.Current.Request.Url.ToString();
sURL = sURL.Replace("https://", "http://");
System.Web.HttpContext.Current.Response.Redirect(sURL);
}
}
}
}

No comments: