Get Array of Bytes from ASP.NET WEB API to be rendered in PDF using React/Redux - pdf

I have this action on my WEBAPI´s controller using .NET that returns bytes from SSRS report link:
public IHttpActionResult GetReport(int? Id)
{
if (Id == null)
{
return NotFound();
}
var reportServer = ConfigurationManager.AppSettings["Report"];
var reportPath = $"{reportServer}/myReport&Id={Id}&rs:Format=PDF";
WebClient Client = new WebClient();
Client.UseDefaultCredentials = true;
byte[] reportBytes = Client.DownloadData(reportPath);
var reportBase64 = Convert.ToBase64String(reportBytes);
return Ok(reportBase64);
}
When i try to generate the PDF from this return, using this react/redux code ( i am using axios)....
//...code
const reportBlob = new Blob([myReportData], { type: "application/pdf" });
const reportBlobUrl = URL.createObjectURL(reportBlob);
window.open(reportBlobUrl);
.....it oppened a new browser´s tab with PDF template BUT showing PDF Error as: 'FAILED TO LOAD PDF document'.
No anwsers anywhere help me resolving this problem. :(
Note: the 'axios' call it´s returning the same array of bytes as the WEBAPI return, so as the 'myReportData' variable.

So i figured out by my self after 4 days. Anyone that needed this functionality (render the SSRS report in PDF within c# code returning PDF bytes from a ASP.NET WEB API, here is the what you have to do within your webapi controller to generate the PDF bytes from report viewer namespace to be sent to your React app using Redux.
WEB API ASP.NET (.NET Framework 4.6 using VS 2019):
note: first option i´ts commented and works the same way the option 2 that i´d prefer.
// return type is Bytes
[HttpGet]
[Route("api/YourReport/{testId}")]
public async Task<IHttpActionResult> GetYourReportAsync(int? testId)
{
if (testId == null)
{
return NotFound();
}
try
{
// 1. Works in this way without '?' on the end of the URL
// URL should be like this: "http://-yourServerName-/reportserver/ReportExecution2005.asmx"
//ReportExecutionService rs = new ReportExecutionService();
//rs.Credentials = CredentialCache.DefaultCredentials;
//rs.Url = "http://-yourServerNamein here-/reportserver/ReportExecution2005.asmx";
//// Render arguments
//byte[] result = null;
//string reportPath = "/-yourReportsFolderName-/-yourReportName-";
//string format = "PDF";
//string historyID = null;
//string devInfo = #"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
//// parameters
//ParameterValue[] parameters = new ParameterValue[1];
//parameters[0] = new ParameterValue();
//parameters[0].Name = "yourId";
//parameters[0].Value = testId.ToString();
//string encoding;
//string mimeType;
//string extension;
//Warning[] warnings = null;
//string[] streamIDs = null;
//ExecutionInfo execInfo = new ExecutionInfo();
//ExecutionHeader execHeader = new ExecutionHeader();
//rs.ExecutionHeaderValue = execHeader;
//execInfo = rs.LoadReport(reportPath, historyID);
//rs.SetExecutionParameters(parameters, "en-us");
//string SessionId = rs.ExecutionHeaderValue.ExecutionID;
//try
//{
//// result type is bytes
// result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
//}
//catch (SoapException)
//{
// throw;
//}
//// below, just in case that you want to save it locally in PDF format.
//try
//{
// FileStream stream = File.Create(#"c:\report.pdf", result.Length);
//
// stream.Write(result, 0, result.Length);
// Console.WriteLine("Result written to the file.");
// stream.Close();
//}
//catch (Exception)
//{
// throw;
//}
//return await Task.Run(() => Ok(result));
// 2. Works this way also with '?' on the end of the URL
// URL should be like this: "http://-yourServerName-/reportserver/ReportExecution2005.asmx?"
using (ReportViewer yourReportViewer = new ReportViewer())
{
yourReportViewer.ProcessingMode = ProcessingMode.Remote;
// get the values from your web.config.
yourReportViewer.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["youtReport"]);
yourReportViewer.ServerReport.ReportPath = $"/yourReportsFolder/yourReport";
;
ReportParameter testPlanIdParameter = new ReportParameter();
testPlanIdParameter.Name = "yourId";
testPlanIdParameter.Values.Add(testId.ToString());
yourReportViewer.ServerReport.SetParameters(new ReportParameter[] { testIdParameter });
byte[] yourReportBytes = yourReportViewer.ServerReport.Render("PDF");
return await Task.Run(() => Ok(yourReportBytes));
}
}
catch (SoapException)
{
throw;
}
catch (Exception)
{
throw ;
}
}
REACT/REDUX nstrong textote: you have to convert the stream of bytes returned from your WEB API Controller into BLOB format to render it in a new brownser tab in PDF format/type.
a. the button action/behavior
<div className="btn-group-vertical" role="group">
<button className="btn btn-sm btn-success ml-2 mb-2" style={{ width: '160px' }} onClick={(e) => openReport(e, testId)}>
<i className="fas fa-print"></i>
Report
</button>
</div>
b. the component ( i am omiting the redux part (reducers, actions, apiservice...)
openReport(event, item) {
event.preventDefault();
this.props.actions.loadReport(item);
this.setState({ Report: this.props.Report });
const ReportData = this.state.Report;
const b64toBlob = (b64Data, contentType = '', sliceSize = 512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, { type: contentType });
return blob;
}
const ReportBlob = b64toBlob(ReportData, 'application/pdf');
const ReportBlobUrl = URL.createObjectURL(ReportBlob);
window.open(ReportBlobUrl);
}

Related

Net core api to upload 1GB size csv file

I have following code segment it works for small file. But if the file is larger then application is loading for long and recieves No 'Access-Control-Allow-Origin' header is present on the requested resource.
[HttpPost]
[ScopeAuthorize(Constants.ClaimScopeSGCanManageAll, Constants.ClaimScopeUserCanManage)]
[DisableRequestSizeLimit, RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue, ValueLengthLimit = int.MaxValue)]
public async Task<IActionResult> UploadFile()
{
if (!Request.Form.Files.Any())
{
throw new Common.Exceptions.ValidationException("Empty file");
}
IFormFile formFile = Request.Form.Files[0];
var csvDatas = new List<PatientsCSVItem>();
using (var reader = new StreamReader(formFile.OpenReadStream()))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
//process csv rows
}
}
PatientCsvLog executionLog = _patientCsvManager.AddOrUpdatePatientsByCsvData(csvDatas, _userManager.GetLoggedUserId(User));
if (executionLog == null)
{
throw new ArgumentNullException(nameof(executionLog));
}
var response = new
{
NumberRecordImported = executionLog.NumberRecordImported,
NumberRecordUpdated = executionLog.NumberRecordUpdated,
NumberRecordDiscarded = executionLog.NumberRecordDiscarded,
DiscardedRecordList = executionLog.DiscardedRecordList
};
return Ok(response);
}

ASP .Net Core file upload - getting form data when [DisableFormValueModelBinding] attribute is in place

I went ahead and implemented an ASP .Net Core file upload controller per the documentation and it requires using a [DisableFormValueModelBinding] attribute for streaming large files. I got that working fine. Unfortunately, when using that attribute it seems to block my JSON properties coming in from the form.
Is there any way to get both the file and the form data here? Here is my controller code (the request.form calls are where I am having issues):
[Route("{caseNbr:int}/Document")]
[ResponseType(typeof(CaseDocumentModel))]
[DisableFormValueModelBinding]
[HttpPost]
public async Task<IActionResult> PostDocument(int caseNbr)
{
string errorTrackingFileName = string.Empty;
try
{
UserSessionModel userSessionModel = SessionExtensions.CurrentUserSession;
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
{
return BadRequest("Bad Request");
}
var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
var section = await reader.ReadNextSectionAsync();
while (section != null)
{
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
if (hasContentDispositionHeader)
{
if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
{
return BadRequest("Bad Request");
}
var fileName = WebUtility.HtmlEncode(contentDisposition.FileName.Value);
errorTrackingFileName = fileName;
var trustedFileNameForFileStorage = fileName; //Path.GetRandomFileName();
var streamedFileContent = await FileHelpers.ProcessStreamedFile(section, contentDisposition, ModelState, _permittedExtensions, _fileSizeLimit);
if (!ModelState.IsValid)
{
return BadRequest("Bad Request");
}
using (var targetStream = System.IO.File.Create(Path.Combine(_tempFilePath, trustedFileNameForFileStorage)))
{
await targetStream.WriteAsync(streamedFileContent);
**//This is where I am having trouble:**
string descrip = HttpContext.Request.Form["Description"].ToString();
string docType = HttpContext.Request.Form["DocType"].ToString() ?? "Document";
bool isGeneralFileUpload = false;
if (string.IsNullOrWhiteSpace(Request.Form["GeneralFileUpload"]) == false && AppHelper.IsBool(Request.Form["GeneralFileUpload"]))
isGeneralFileUpload = bool.Parse(Request.Form["GeneralFileUpload"]);
int transcriptionJobId = 0;
if (string.IsNullOrWhiteSpace(Request.Form["TranscriptionJobId"]) == false && AppHelper.IsNumeric(Request.Form["TranscriptionJobId"]))
transcriptionJobId = int.Parse(Request.Form["TranscriptionJobId"]);
CaseDocumentModel createdCaseDocumentModel;
if (docType.Equals("Dictation"))
createdCaseDocumentModel = DictationRepository.ProcessDictationFile(userSessionModel.DBID, caseNbr, _tempFilePath, fileName, userSessionModel);
else if (isGeneralFileUpload)
createdCaseDocumentModel = DashboardAdjusterRepository.CreateGeneralFileUploadDocument(_tempFilePath, fileName, userSessionModel, docType, descrip);
else if (docType.Equals("Transcription"))
createdCaseDocumentModel = TranscriptionRepository.UploadTranscriptionFile(userSessionModel.DBID, _tempFilePath, fileName, userSessionModel, transcriptionJobId);
else
createdCaseDocumentModel = CaseRepository.CreateCaseDocumentRecord(userSessionModel.DBID, caseNbr, descrip, docType, _tempFilePath, fileName, userSessionModel);
return Ok(createdCaseDocumentModel);
}
}
// Drain any remaining section body that hasn't been consumed and
// read the headers for the next section.
section = await reader.ReadNextSectionAsync();
}
}
catch (Exception ex)
{
AppHelper.WriteErrorLog("CaseController PostDocument failed due to " + ex.Message + " case number was " + caseNbr + " file name was " + errorTrackingFileName);
return BadRequest("Bad Request");
}
return BadRequest("Bad Request");
}
Here is a sample call with Postman:
Screen shot of Postman

Azure Logic Apps internal server error 500

Am trying to create a an azure function that is triggered in a Logic Apps,
The functions purpose is to web crawl certain web sites, take the desired information, compare that with a a SQL Server database in Azure, compare if we already have that information if not add it.
My issue is that when ever i run it I get the Server 500 error, I assume its accessing the database that cause. Help?
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log
)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string RequestBody = await new StreamReader(req.Body).ReadToEndAsync();
{
return await CrawlBlog(0, RequestBody);
}
}
private static async Task<IActionResult> CrawlBlog(int Picker, string req)
{
int BlogPicker = Picker;
string TheResult = req;
//Get the url we want to test
var Url = "";
if (BlogPicker == 0)
{
Url = "*********";
}
else if (BlogPicker == 1)
{
Url = "*********";
}
/*
else if (BlogPicker == 2)
{
Url = "https://azure.microsoft.com/en-in/blog/?utm_source=devglan";
}
*/
else
{
TheResult = "False we got a wrong pick";
return (ActionResult)new OkObjectResult
( new {TheResult });
}
var httpClient = new HttpClient();
var html = await httpClient.GetStringAsync(Url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
//a list to add all availabel blogs we found
var Blog = new List<BlogStats>();
switch (BlogPicker)
{
case 0:
{
var divs =
htmlDocument.DocumentNode.Descendants("div")
.Where(node => node.GetAttributeValue("class", "").Equals("home_blog_sec_text")).ToList();
foreach (var divo in divs)
{
var Blogo = new BlogStats
{
Summary = divo.Descendants("p").FirstOrDefault().InnerText,
Link = divo.Descendants("a").FirstOrDefault().ChildAttributes("href").FirstOrDefault().Value,
Title = divo.Descendants("a").FirstOrDefault().InnerText
};
Blog.Add(Blogo);
}
break;
}
case 1:
{
var divs =
htmlDocument.DocumentNode.Descendants("div")
.Where(node => node.GetAttributeValue("class", "").Equals("post_header_title two_third last")).ToList();
foreach (var divo in divs)
{
//string TheSummary = "we goofed";
var ThePs = divo.Descendants("p").ToList();
var Blogo = new BlogStats
{
Summary = ThePs[1].GetDirectInnerText(),
Link = divo.Descendants("a").LastOrDefault().ChildAttributes("href").FirstOrDefault().Value,
Title = divo.Descendants("a").FirstOrDefault().InnerText
};
Blog.Add(Blogo);
}
break;
}
}
TheResult = await SqlCheck(Blog[0].Title, Blog[0].Summary, Blog[0].Link); //error 500
return (ActionResult)new OkObjectResult
(
new
{
TheResult
}
);
}
public static async Task<string> SqlCheck(string Tit, string Sumy, string Lin)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "flygon.database.windows.net";
builder.UserID = "*****";
builder.Password = "********";
builder.InitialCatalog = "torkoal";
System.Data.DataSet ds = new System.Data.DataSet();
SqlConnection connection = new SqlConnection(builder.ConnectionString);
connection.Open();
SqlCommand CheckCommand = new SqlCommand("SELECT * FROM TableBoto WHERE Link = #id3 ", connection);
CheckCommand.Parameters.AddWithValue("#id3", Lin);
SqlDataAdapter dataAdapter = new SqlDataAdapter(CheckCommand);
dataAdapter.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i > 0)
{
return $" We got a Duplicates in title : {Tit}";
}
try
{
{
string query = $"insert into TableBoto(Title,Summary,Link) values('{Tit}','{Sumy}','{Lin}');";
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = await command.ExecuteReaderAsync();
reader.Close();
}
}
catch (SqlException)
{
// Console.WriteLine(e.ToString());
}
connection.Close();
return $" Success Ign +{Tit} + Ign {Sumy}+ Ign {Lin} Ign Success SQL ";
}
}
500 HTTP status code is a generic code which means that the server was not able to process the request due to some issues, First step would be to add some exception handling to your function and see if the failure occurs and where it occurs.
On Side note, you should not use HTTP client in the way used in the code, you should not new it up every time your function executes, this client should be static in nature. Refer Manage connections in Azure Functions

Win Phone 8.1 : OutOfMemory Exception while downloading large files (30 MB)

I am creating a universal Windows Phone 8.1 App. I am trying to download the file and view it into launcher. I works for small file less than 15 MB files. But when file size is more than 15 MB, I got the out of memory exception.
async private Task<object> GetMailAttachments(string attachNotify)
{
try
{
cmdBarMailItem.IsEnabled = false;
if (await Device.IsNetworkAvailable())
{
cts = new CancellationTokenSource();
// Ignore SSL Certificate which is untrusted,expired and has invalid hostname.
var filter = new HttpBaseProtocolFilter() { AllowUI = false };
filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Untrusted);
filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Expired);
filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.InvalidName);
// Start calling the soap service #userGetAttachmentByIndex
using (var client = new System.Net.Http.HttpClient(new WinRtHttpClientHandler(filter)))
{
//Prepare parameters which is to be post via soap envelope.
List<KeyValuePair<string, string>> parameter = new List<KeyValuePair<string, string>>();
parameter.Add(new KeyValuePair<string, string>("sessionId", GlobalInfo.SessionID));
parameter.Add(new KeyValuePair<string, string>("attachmentIndex", attachNotify.Split('|')[1].ToString()));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
client.DefaultRequestHeaders.Add("SOAPAction", "userGetAttachmentByIndex");
var postContent = new StringContent(StringHelper.ConstructSoapRequest(parameter, "userGetAttachmentByIndex"), Encoding.UTF8, "text/xml");
// Getting response from soap service
var response = await client.PostAsync(new Uri(AppEnv.ServiceEndPoint), postContent, cts.Token);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string soapResponse = await response.Content.ReadAsStringAsync();
var soap = XDocument.Parse(soapResponse);
XNamespace ns = "http://service.webservice.cryoserver.ci";
var base64BinaryStr = soap.Descendants(ns + "userGetAttachmentByIndexResponse").First().Descendants(ns + "return").First().Descendants(ns + "attachmentType").First().Descendants(ns + "binaryData").First().Descendants(ns + "base64Binary").First().Value;
await saveStringToLocalFile(base64BinaryStr);
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("myTest.pdf");
bool x = await Windows.System.Launcher.LaunchFileAsync(file);
return x;
}
}
}
cmdBarMailItem.IsEnabled = true;
}
catch (TaskCanceledException)
{
PopupRetrieve.IsOpen = false;
ProgressBar.IsVisible = false;
cmdBarMailItem.IsEnabled = true;
}
catch(Exception ex)
{
cmdBarMailItem.IsEnabled = true;
ProgressBar.IsVisible = false;
MessageBox.Show(AlertType.Connectivity);
}
return null;
}
async Task saveStringToLocalFile(string content)
{
try
{
// saves the string 'content' to a file 'filename' in the app's local storage folder
// byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(content.ToCharArray());
byte[] byteArray = Convert.FromBase64String(content);
// create a file with the given filename in the local folder; replace any existing file with the same name
StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("myTest.pdf", CreationCollisionOption.ReplaceExisting);
// write the char array created from the content string into the file
using (var stream = await file.OpenStreamForWriteAsync())
{
stream.Write(byteArray, 0, byteArray.Length);
stream.Flush();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I got the exception on executing the line
string soapResponse = await response.Content.ReadAsStringAsync();
Anybody have an idea why the exception occurs ? What could be possible solution to fix it.
Any help would be appriciable. :)

Is there a way to change command-line arguments in a signed RDP file?

I need to specify command line arguments for a RemoteApp using the remoteapplicationcmdline property of a signed RDP file.
However, after the RDP file is signed, I cannot change the value for remoteapplicationcmdline. If I remove remoteapplicationcmdline from the signscope, I can't specify any parameters.
This is the same question asked by Chupkb on Technet.
I might be able to do as Naraen suggested on this question, but before I do that I'm hoping there is a simpler way.
The route I eventually took was to gather the command line arguments from a previous page (perhaps a login page) and then on the next page I dynamically create an rdp file, sign it, and embed the contents into the javascript on the html page. The code below is ASP.NET and c# but it should be a good starting point for other languages as well.
First, the html (notice the <%= RdpFileContents %> in LaunchRemoteApp():
<html>
<head>
<title>RD Web Access</title>
<script type="text/javascript" language="javascript">
var g_workspace = null;
var workspaceID = "[RemoteAppMachineName]";
var domainUserName = "[RemoteAppMachineName]\\[RemoteAppUserName]";
// This is the certificate's thumbprint.
var rdpCertificates = "04b83503a710cad89f25d3e15a48245f490773c4";
var machineType = "public";
function onAutoDisconnect() {
var workspace = null;
if (workspaceID != null && workspaceID != "") {
try {
workspace = GetWorkspaceObject();
workspace.ClearWorkspaceCredential(workspaceID);
}
catch (objException) {
workspace = null;
}
}
}
function onUserDisconnect() {
var workspace = null;
try {
workspace = GetWorkspaceObject();
workspace.DisconnectWorkspace(workspaceID);
}
catch (objException) {
workspace = null;
}
}
function onAuthenticated() {
var workspace = null;
var isOnAuthenticatedCalled = false;
if (workspaceID != null && workspaceID != "") {
try {
workspace = GetWorkspaceObject();
workspace.StartWorkspace(workspaceID, domainUserName, password, rdpCertificates, 1200000, 0);
var countUnauthenticatedCredentials = true;
var isWorkspaceCredentialSpecified = workspace.IsWorkspaceCredentialSpecified(workspaceID, countUnauthenticatedCredentials);
if (isWorkspaceCredentialSpecified) {
isOnAuthenticatedCalled = true;
workspace.OnAuthenticated(workspaceID, domainUserName);
}
setTimeout("onAutoDisconnect()", 1200000);
}
catch (objException) {
workspace = null;
var errorCode = (objException.number & 0xFFFF);
if (isOnAuthenticatedCalled) {
if (errorCode == 183) {
// 183 = ERROR_ALREADY_EXISTS.
}
else if (errorCode == 1168) {
// 1168 = ERROR_NOT_FOUND.
}
}
}
}
}
function GetWorkspaceObject() {
var objClientShell = null;
if (g_workspace == null) {
objClientShell = new ActiveXObject("MsRdpWebAccess.MsRdpClientShell");
g_workspace = objClientShell.MsRdpWorkspace;
}
return g_workspace;
}
function LaunchRemoteApp() {
var msRdpClientShell = document.all.MsRdpClient;
msRdpClientShell.PublicMode = true;
msRdpClientShell.RdpFileContents = <%= RDPContents %>;
try {
msRdpClientShell.Launch();
}
catch (e) {
throw e;
}
}
</script>
</head>
<body onload="onAuthenticated();">
<form name="form1" method="post" action="default.aspx" id="form1">
<object name="MsRdpClient" width="0" height="0" id="MsRdpClient" classid="CLSID:6A5B0C7C-5CCB-4F10-A043-B8DE007E1952" type="application/x-oleobject"></object>
YourAppName
</form>
</body>
</html>
Then I created a template RDP file with all the correct content. Notice the remoteapplicationcmdline value has some dummy command line parameters (FirstName, LastName, CustomerID). You'll replace these as appropriate in your code.
redirectclipboard:i:0
redirectposdevices:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:0
devicestoredirect:s:
drivestoredirect:s:
redirectdrives:i:0
session bpp:i:32
prompt for credentials:i:0
span monitors:i:0
use multimon:i:0
remoteapplicationmode:i:1
server port:i:3389
allow font smoothing:i:1
promptcredentialonce:i:1
authentication level:i:2
gatewayusagemethod:i:2
gatewayprofileusagemethod:i:0
gatewaycredentialssource:i:0
full address:s:[RemoteAppMachineName]
alternate shell:s:||[YourAppName]
remoteapplicationprogram:s:||[YourAppName]
gatewayhostname:s:
remoteapplicationname:s:[YourAppName]
remoteapplicationcmdline:s:/FirstName:{0} /LastName:{1} /CustomerID:{2}
Workspace Id:s:[RemoteAppMachineName]
Then in the code behind, I load the template rdp file, replace the command line arguments, write to a temp rdp file, sign the temp rdp file, and embed the resulting contents into the javascript at <%= RdpFileContents %>
private string GetRDPContents()
{
FileInfo fileInfo = new FileInfo(Path.Combine(Server.MapPath("~"), "Template.rdp"));
string[] unsignedRDPContents = File.ReadAllLines(fileInfo.FullName);
for (int i = 0; i < unsignedRDPContents.Length; i++)
{
if (unsignedRDPContents[i].IndexOf("remoteapplicationcmdline") != -1)
unsignedRDPContents[i] = String.Format(unsignedRDPContents[i], FirstName, LastName, CustomerID);
}
string tempFileName = Path.Combine(Server.MapPath("~"), Guid.NewGuid().ToString() + ".rdp");
File.WriteAllLines(tempFileName, unsignedRDPContents);
InvokeRDPSign(tempFileName, certificateThumbprint);
string[] signedRDPContents = File.ReadAllLines(tempFileName);
File.Delete(tempFileName);
// This escape function is identical to the javascript escape function.
return String.Format("unescape('{0}')", Microsoft.JScript.GlobalObject.escape(String.Join("\r\n", signedRDPContents)));
}
// This is the certificate's thumbprint.
private const string certificateThumbprint = "04a85503a810cad89f25d3f14c48245f499773c4";
private void InvokeRDPSign(string fileName, string certificateThumbPrint)
{
Process rdpSign = new Process();
rdpSign.StartInfo.FileName = "rdpsign.exe";
rdpSign.StartInfo.Arguments = String.Format("/sha1 {0} \"{1}\"", certificateThumbPrint, fileName);
rdpSign.StartInfo.UseShellExecute = false;
rdpSign.StartInfo.RedirectStandardOutput = true;
rdpSign.StartInfo.RedirectStandardInput = true;
rdpSign.StartInfo.RedirectStandardError = true;
rdpSign.StartInfo.WorkingDirectory = Server.MapPath("~");
rdpSign.Start();
string signingOutput = rdpSign.StandardOutput.ReadToEnd();
rdpSign.WaitForExit();
int exitCode = rdpSign.ExitCode;
}
The final javascript will look something like this:
function LaunchRemoteApp() {
var msRdpClientShell = document.all.MsRdpClient;
msRdpClientShell.PublicMode = true;
msRdpClientShell.RdpFileContents = unescape('redirectclipboard%3Ai%3A0%0D%0A%0D%0Aredirectposdevices%3Ai%3A0%0D%0A%0D%0Aredirectprinters%3Ai%3A0%0D%0A%0D%0Aredirectcomports%3Ai%3A0%0D%0A%0D%0Aredirectsmartcards%3Ai%3A0%0D%0A%0D%0Adevicestoredirect%3As%3A%0D%0A%0D%0Adrivestoredirect%3As%3A%0D%0A%0D%0Aredirectdrives%3Ai%3A0%0D%0A%0D%0Asession%20bpp%3Ai%3A32%0D%0A%0D%0Aprompt%20for%20credentials%3Ai%3A0%0D%0A%0D%0Aspan%20monitors%3Ai%3A0%0D%0A%0D%0Ause%20multimon%3Ai%3A0%0D%0A%0D%0Aremoteapplicationmode%3Ai%3A1%0D%0A%0D%0Aserver%20port%3Ai%3A3389%0D%0A%0D%0Aallow%20font%20smoothing%3Ai%3A1%0D%0A%0D%0Apromptcredentialonce%3Ai%3A1%0D%0A%0D%0Aauthentication%20level%3Ai%3A2%0D%0A%0D%0Agatewayusagemethod%3Ai%3A2%0D%0A%0D%0Agatewayprofileusagemethod%3Ai%3A0%0D%0A%0D%0Agatewaycredentialssource%3Ai%3A0%0D%0A%0D%0Afull%20address%3As%3A[YourMachineName]%0D%0A%0D%0Aalternate%20shell%3As%3A%7C%7C[YourAppName]%0D%0A%0D%0Aremoteapplicationprogram%3As%3A%7C%7C[YourAppName]%0D%0A%0D%0Agatewayhostname%3As%3A%0D%0A%0D%0Aremoteapplicationname%3As%3A[YourAppName]%0D%0A%0D%0Aremoteapplicationcmdline%3As%3A/FirstName%3AJon%20/LastName%3ADoe%20/CustomerID%3A7921385%0D%0A%0D%0Aworkspace%20id%3As%3A[YourMachineName]%0D%0A%0D%0Aalternate%20full%20address%3As%3A[YourMachineName]%0D%0A%0D%0Asignscope%3As%3AFull%20Address%2CAlternate%20Full%20Address%2CServer%20Port%2CGatewayHostname%2CGatewayUsageMethod%2CGatewayProfileUsageMethod%2CGatewayCredentialsSource%2CPromptCredentialOnce%2CAlternate%20Shell%2CRemoteApplicationProgram%2CRemoteApplicationMode%2CRemoteApplicationName%2CRemoteApplicationCmdLine%2CPrompt%20For%20Credentials%2CAuthentication%20Level%2CRedirectDrives%2CRedirectPrinters%2CRedirectCOMPorts%2CRedirectSmartCards%2CRedirectPOSDevices%2CRedirectClipboard%2CDevicesToRedirect%2CDrivesToRedirect%0D%0A%0D%0Asignature%3As%3AAQABAAEAAADgEgAAMIIS3AYJKoZIhvcNAQcCoIISzTCCEskCAQExCzAJBgUrDgMC%20%20GgUAMAsGCSqGSIb3DQEHAaCCEQMwggPFMIICraADAgECAhACrFwmagtAm48LefKu%20%20RiV3MA0GCSqGSIb3DQEBBQUAMGwxCzAJBgNVBAYTAlVTMRUwEwYDVQQKEwxEaWdp%20%20Q2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xKzApBgNVBAMTIkRp%20%20Z2lDZXJ0IEhpZ2ggQXNzdXJhbmNlIEVWIFJvb3QgQ0EwHhcNMDYxMTEwMDAwMDAw%20%20WhcNMzExMTEwMDAwMDAwWjBsMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNl%20%20cnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdp%20%20Q2VydCBIaWdoIEFzc3VyYW5jZSBFViBSb290IENBMIIBIjANBgkqhkiG9w0BAQEF%20%20AAOCAQ8AMIIBCgKCAQEAxszlc+b71LvlLS0ypt/lgT/JzSVJtnEqw9WUNGeiChyw%20%20X2mmQLHEt7KP0JikqUFZOtPclNY823Q4pErMTSWC90qlUxI47vNJbXGRfmO2q6Zf%20%20w6SE+E9iUb74xezbOJLjBuUIkQzEKEFV+8taiRV+ceg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K8UgCSc2qgS8Kry/F/TlzSCe2fBjRPMwzg%20%20FdSd62d5QmneyaJ4bjyA+leyl+aLAUke4XLJM1qiPPmOE+R2Pb/97XADUW4kdbGt%20%207bvAsacHW0XQSgyU4w3QozSw0ot88y9lM2tWX9XtffTNsAW8GiGDR2q94ZzGulxn%20%20VMk1usccLy4rtzDs66y1NZjm8bknYdEOWhsod+A/4HGHgL/Z3febiyhTaIyp9hMY%20%20x0xLArDbcjGCAaEwggGdAgEBMHowZjELMAkGA1UEBhMCVVMxFTATBgNVBAoTDERp%20%20Z2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3LmRpZ2ljZXJ0LmNvbTElMCMGA1UEAxMc%20%20RGlnaUNlcnQgSGlnaCBBc3N1cmFuY2UgQ0EtMwIQDyZO5SPrkaGllR9mdxiQpTAJ%20%20BgUrDgMCGgUAMA0GCSqGSIb3DQEBAQUABIIBAJGNkWq+QeMYRPJZG3+Kwen96uy3%20%208V+6iE5pnGAFzrBPuN1BhvmkhiSDKLcT4ZSI07PNAsuZFLFz7g8yAuVWyWkfCJkn%20%20TAzj7kFCZBUP82ousA+Pp0VMdmfKjKfqOlJKPMpNJ6AKSHT1aAw4N2ZuzhnddZ/I%20%20HzrOoIJ9McvrMzGQSmhA4MefBg1ppqbudo1QCvVfOdwWg5KijHKIkVOe9BI8Dyic%20%20+cLu3iJ1jF3IMwMQb2Q4JX/FgIFMy9Hf12uhx9HP0JnR2sp5AnwfAGqPj2RSCmoA%20%20Px1XJxuea7WjMBnfTCUuQjuD/hfYdyTmj95G2nuZZM2Za4438JWPMPMW5ro%3D%20%20%0D%0A');
try {
msRdpClientShell.Launch();
}
catch (e) {
throw e;
}
}
I tried to escape the rdp contents myself but could never get it to work. If you don't have access to Microsoft.JScript.GlobalObject.escape() function, you can try it yourself. Here are some of the character entity references I know of:
Replace : with %3A
Replace | with %7C
Replace \r\n with %0D%0A
Replace spaces with %20
No extra spaces after lines.