Monday, June 29, 2015

RDP/RDC from asp.net


We can download .rdp file to connect to some server. Following code will create an rdp file and download that on client’s machine. Opening this file, user will be able to rdp specified server.

Foe this we need to create an rdp file in some download folder. Create a download folder and place a dummy download rdp file:


ASPX Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr style="background-color: #FF9900; color: #FFF">
                    <td colspan="2">RDP
                    </td>
                </tr>
                <tr style="background-color: #ECECEC; color: #000">
                    <td>Description
                    </td>
                    <td>Link
                    </td>
                </tr>
                <tr>
                    <td>RDP1:
                    </td>
                    <td>
                        <asp:LinkButton runat="server" OnClick="lnkDownload_Click">127.0.0.1</asp:LinkButton>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

C# Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
        string fileName = "Default.rdp";
        string filePath = Server.MapPath("~/Download/" + fileName);
        LinkButton linkButton = (LinkButton)sender;
        UpdateRDPFile(linkButton.Text, filePath);
        DownLoadFile(filePath);
    }

    private void UpdateRDPFile(string IPAddress, string filePath)
    {
        StringBuilder newFile = new StringBuilder();
        string temp = "";
        string[] fileToUpdate = File.ReadAllLines(filePath);
        foreach (string line in fileToUpdate)
        {
            if (line.Contains("full address:s:"))
            {
                temp = temp.Insert(0, "full address:s:" + IPAddress);
                newFile.Append(temp + "\r\n");
                continue;
            }
            newFile.Append(line + "\r\n");
        }

        File.WriteAllText(filePath, newFile.ToString());
    }

    private void DownLoadFile(string filePath)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", ("attachment; filename=" + file.Name));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
    }

}

No comments:

Post a Comment