Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to issue a post request webclient under specific jwt context

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?

like image 561
Alex Gordon Avatar asked Feb 11 '18 17:02

Alex Gordon


1 Answers

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);

//...
like image 182
Nkosi Avatar answered Nov 07 '22 11:11

Nkosi