How can I issue a POST request under the context below using webclient?
I am able to successfully authenticate and retrieve a jwt
token from ADFS:
using (var client = new WebClient())
{
var data = Encoding.UTF8.GetBytes(rstXml);
client.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");
var responseData =
client.UploadData($"https://{adfsServer}/adfs/services/trust/13/usernamemixed", data);
var rstr = Encoding.UTF8.GetString(responseData);
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(rstr);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
nsmgr.AddNamespace("wsu",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
jwt = xmlDoc.SelectSingleNode("//wsse:BinarySecurityToken", nsmgr).InnerText; // JWT
}
How do I issue a POST request under this same context?
Authorization header should be able to be added to the client to make authorized requests.
//Adding authorization token to header
client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + jwt);
//Resetting content type to be sent if we wanted to POST JSON
client.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
//POST json
var json = "{ \"Key\": \"Hello World\" }";
var response = client.UploadString("__url_here__", json);
//...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With