Jun 5, 2014

Posted in , , , ,

ASP.NET 4.5.2 Hosting - HostForLIFE :: Run Your Web Apps with the Web.config

Have you think that you could run your web apps with the Web.config being transformed according to the current solution configuration for Debug or Release directly from your Visual Studio? Now, we are going to try run web apps from web.config from ASP.NET 4.5.2 Hosting.
http://www.hostforlife.eu/European-ASPNET-Hosting-Free-Trial
The trick is you should create a file named Web.Base.config, besides the existing Web.Debug.config and Web.Release.config. This file will be the same to your old Web.config, as it will be the base for the transformation. Then the result files is Web.config, Web.Base.config, Web.Debug.config and Web.Release.config

Then, Add the following config to the bottom of your .csproj file, just before the closing </Project> tag: 
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
<TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>

The \v11.0\ part is for Visual Studio 2012. If you're using VS 2010 you change this to \v10.0\.

Now you can build and run your project or publish it all depending on what Solution Configuration you have selected. Debug or Release. Just press your CTRL+SHIFT+B and see the Web.config-file be transformed.

I recommend that you leave Web.config out of your Version Control. This solution for transforming the Web.config on the fly would mean that this specific file would constantly be modified and if one person is working with Debug and another person is working with Release the Web.config would probably get big changes constantly. 

Apr 21, 2014

Posted in , , ,

Free ASP.NET 4.5 Hosting - HostForLIFE.eu :: How to share Session securely between Classic ASP and ASP.NET

Looking at the Google search results, it seems that there has been plenty of demand to share Session values between Classic ASP and ASP.NET. This is usually a result of supporting legacy Classic ASP.NET with new development in ASP.NET 4.5 Hosting. Most of the solutions I found either did not handle sharing securely or required modifications to the Classic ASP code (such as the top ranking Microsoft solution). Deep into the search results I found what seems like a great solution but doesn't seem to have had much support in the form of comments and social shares.


Like most of the solutions, it uses a Classic ASP script to return requested session variables. But it does this securely by locking down this script to internal requests only (by IP address), and retrieving by a server-side HTTP request. As a server-side request would usually use the user Session of the web server itself rather than the user's Session, the author of this code cleverly retrieves the Classic ASP cookie and passes it in the request headers. A very clever solution.

The only improvement I could suggest is that the IP address could be faked so instead I would possibly suggest using a password protected folder or some other method of authentication. I also found that when I tried to access the Classic ASP session using the HTTPS protocol it would not work. The only way I could get it to work was to change the IIS setting "New ID On Secure Connect" to false which allows the use of the same Session in both HTTP and HTTPS.

I have used that concept and made my own version that allows you to get or set a Classic ASP Session variable from ASP.NET:

AspSession.cs
using System;
using System.IO;
using System.Net;
using System.Web;

public class AspSession
{
    public static object Get(string name)
    {
        HttpContext context = HttpContext.Current;
        object value = null;
        String[] cookies = context.Request.Cookies.AllKeys;
        for (int i = 0; i < cookies.Length; i++)
        {
            HttpCookie cookie = context.Request.Cookies[cookies[i]];
            if (cookie.Name.StartsWith("ASPSESSION"))
            {
                System.Uri uri = context.Request.Url;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Scheme + "://" + uri.Host + ":" + uri.Port.ToString() + "/Services/AspSession.asp?mode=get&name=" + name);
                request.Headers.Add("Cookie: " + cookie.Name + "=" + cookie.Value);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader readStream = new StreamReader(responseStream, encode);
                value = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
                break;
            }
        }
        return value;
    }

    public static void Set(string name, object value)
    {
        HttpContext context = HttpContext.Current;

        String[] cookies = context.Request.Cookies.AllKeys;

        for (int i = 0; i < cookies.Length; i++)
        {
            HttpCookie cookie = context.Request.Cookies[cookies[i]];

            if (cookie.Name.StartsWith("ASPSESSION"))
            {
                System.Uri uri = context.Request.Url;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Scheme + "://" + uri.Host + ":" + uri.Port.ToString() + "/Services/LegacySession.asp?mode=set&name=" + context.Server.UrlEncode(name) + "&value=" + context.Server.UrlEncode(value.ToString()));
                request.Headers.Add("Cookie: " + cookie.Name + "=" + cookie.Value);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                break;
            }
        }
    }
}

/services/aspsession.asp

<%
Dim strMode, strName, strValue

If Request.ServerVariables("REMOTE_ADDR") = Request.ServerVariables("LOCAL_ADDR") Then
    strMode = Request.QueryString("mode")
    strName = Request.QueryString("name")
    If strMode = "get" Then
        Response.Write(Session(strName))
    ElseIf strMode = "set" Then
        strValue = Request.QueryString("value")
        Session(strName) = strValue
    End If
End If
%>