Monday, January 18, 2010

ASP.NET MVC – Simple Remote Desktop connection controller

With the advancement of larger datacenters and upcoming cloud solutions it can become tedious to remember all the machine names to establish RDP connection for maintenance and debugging. Usually, after while in enterprise solutions, you start creating some maintenance applications listing all the hosts involved. Be it for gathering log-information, package deployment, … What I missed was a easy way to just click on a link and open a RDP session. Our continuous integration server (TeamCity from JetBrains) has a solution for this. It just simply streams a dynamic generated .RDP file. Here is a simple solution to implement this in ASP.NET MVC.

Controller

using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcTestWebApplication.Controllers
{
   public class RdpController : Controller
   {

       //
       // GET: /Rdp/hostname

       public ActionResult Index(string hostname)
       {
           // not hostname specified -- return a message
           if (string.IsNullOrEmpty(hostname))
               return View();

           var content =
               string.Format(
                   @"
screen mode id:i:1
desktopwidth:i:1024
desktopheight:i:768
smart sizing:i:1
session bpp:i:16
compression:i:1
keyboardhook:i:2
audiomode:i:2
redirectdrives:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:1
displayconnectionbar:i:1
autoreconnection enabled:i:1
alternate shell:s:
shell working directory:s:
disable wallpaper:i:1
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:1
disable cursor setting:i:0
bitmapcachepersistenable:i:1
full address:s:{0}
domain:s:null
",
                   hostname);


           return new FileContentResult(
               Encoding.UTF8.GetBytes(content),
               "application/rdp")
                      {
                          FileDownloadName = hostname + ".rdp"
                      };
       }



   }
}

Global.asax

Register the Routes in Application_Start in global.asax

     routes.MapRoute(
                   "Rdp",
                   "Rdp/{hostname}",
                    new { controller = "Rdp",
                          action = "Index" });

Use it in a View

<%=Html.ActionLink("RDP localhost","Index", "Rdp",
                    new { hostname = "localhost"}, new {}) %>

A overview of the RDP file settings can be found here.