Come passare da Http a Https tramite un attributo ed un modulo Http

Scritto da  Alessandro Mostarda il giovedì 13 gennaio 2011  •  Linguaggio: C#,VB   • Livello: 200


Spesso abbiamo necessità di avere una o più pagine del nostro web Site che utilizzano un certificato e, pertanto, che usano il protocollo HTTPS.
Per fare ciò ci sono vari modi, ed uno di questi è quello di marcare le pagine con un attributo che indica se la pagina deve utilizzare il protocollo HTTPS o meno.

Prima di tutto dobbiamo creare un attributo:

C#

public class TagHttp: Attribute
{
public bool IsHttps { get; set; }
}


VB.NET

Public Class TagHttp
Inherits Attribute
Public Property IsHttps As Boolean
End Class



Dopo di che creiamo un paio di pagine decorandole con l'attributo

C#

[TagHttp(IsHttps = true)]
public partial class PageHttps: Page
{
}

[TagHttp(IsHttps=false)]
public partial class PageHttp : Page
{
}


VB.NET

<TagHttp(IsHttps:=False)>
Public Class PageHttp
Inherits Page
End Class
<TagHttp(IsHttps:=True)>
Public Class PageHttps
Inherits Page
End Class



e per finire ecco il codice del modulo http per effettuare il test della pagina ed utilizzare il protocollo corretto

C#

public class ModuleHttp :
IHttpModule
{
public void Dispose()
{
//
}

public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}

void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
TagHttp tag = (TagHttp)page.GetType().GetCustomAttributes(typeof(TagHttp), true).First();
if (HttpContext.Current.Request.IsSecureConnection)
{
if (!tag.IsHttps)
{
HttpContext.Current.Response.RedirectPermanent(HttpContext.Current.Request.Url.AbsoluteUri.Replace("https://", "http://"), true);
}
}
else
{
if (tag.IsHttps)
{
HttpContext.Current.Response.RedirectPermanent(HttpContext.Current.Request.Url.AbsoluteUri.Replace("https://", "http://"), true);
}
}
}
}
}


VB.NET

Public Class ModuleHttp
Implements IHttpModule
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub

Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
AddHandler context.PreRequestHandlerExecute, AddressOf context_PreRequestHandlerExecute
End Sub

Private Sub context_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
Dim page As Page = TryCast(HttpContext.Current.CurrentHandler, Page)
If page IsNot Nothing Then
Dim tag As TagHttp = DirectCast(page.GetType().GetCustomAttributes(GetType(TagHttp),True).First(), TagHttp)
If HttpContext.Current.Request.IsSecureConnection Then
If Not tag.IsHttps Then
HttpContext.Current.Response.RedirectPermanent(HttpContext.Current.Request.Url.AbsoluteUri.Replace("https://", "http://"), True)
End If
Else
If tag.IsHttps Then
HttpContext.Current.Response.RedirectPermanent(HttpContext.Current.Request.Url.AbsoluteUri.Replace("http://", "http://"), True)
End If
End If
End If
End Sub
End Class



P.S. Ricordarsi di registrare il modulo all'interno del file web.config...


Tags: Reflection,ASP.NET

 
x