Inject variable into html input tag value using Html Agility Pack C#

c# html html-agility-pack saml

Question

Is it possible to introduce a variable into the chosen node using the C# HTML Agility Pack?

After creating my HTML form, loading it, and choosing the desired input node, I now want to insert a SAML Response into the value field.

Here is some of the code I have, starting with the HTML file:

<html xmlns="http://www.w3.org/1999/xhtml">
<head  id="Head1" runat="server">
    <title></title>
</head>
<body runat="server" id="bodySSO">
    <form id="frmSSO" runat="server" enableviewstate="False">
        <div style="display:none" >
            <input id="SAMLResponse" name="SAMLResponse" type="text" runat="server" enableviewstate="False" value=""/>
            <input id="Query" name="Query" type="text" runat="server" enableviewstate="False" value=""/>
        </div>
    </form>
</body>
</html>

and the following code loads the HTML content and chooses the desired node:

public static string GetHTMLForm(SamlAssertion samlAssertion)
{
    HtmlAgilityPack.HtmlDocument HTMLSamlDocument = new HtmlAgilityPack.HtmlDocument();
    HTMLSamlDocument.Load(@"C:\HTMLSamlForm.html");
    HtmlNode node = HTMLSamlDocument.DocumentNode.SelectNodes("//input[@id='SAMLResponse']").First();

    //Code that will allow me to inject into the value field my SAML Response
}

EDIT:

Okay, so using the following, I was able to successfully inject the SAML Response packet into the HTML Input Tag's "value" field:

HtmlAgilityPack.HtmlDocument HtmlDoc = new HtmlAgilityPack.HtmlDocument();
String SamlInjectedPath = "C:\\SamlInjected.txt";
HtmlDoc.Load(@"C:\HTMLSamlForm.txt");
var SAMLResposeNode = HtmlDoc.DocumentNode.SelectSingleNode("//input[@id='SAMLResponse']").ToString();
SAMLResposeNode = "<input id='SAMLResponse' name='SAMLResponse' type='text' runat='server' enableviewstate='False' value='" + samlAssertion + "'/>";

It's simply a matter of adding the injected tag back into the original HTML content, I suppose.

1
1
9/22/2016 3:51:20 PM

Accepted Answer

Okay, I found a solution by utilizing the following:

HtmlAgilityPack.HtmlDocument HtmlDoc = new HtmlAgilityPack.HtmlDocument();
HtmlDoc.Load(@"C:\HTMLSamlForm.html");
var SamlNode = HtmlNode.CreateNode("<input id='SAMLResponse' name='SAMLResponse' type='text' runat='server' enableviewstate='False' value='" + samlAssertion + "'/>");
foreach (HtmlNode node in HtmlDoc.DocumentNode.SelectNodes("//input[@id='SAMLResponse']"))
{
    string value = node.Attributes.Contains("value") ? node.Attributes["value"].Value : "&nbsp;";
    node.ParentNode.ReplaceChild(SamlNode, node);
}

I then produce the new HTML file using the following to verify its contents:

System.IO.File.WriteAllText(@"C:\SamlInjected.txt", HtmlDoc.DocumentNode.OuterHtml);
0
9/26/2016 12:30:52 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow