如何在特定的 jwt 上下文下发出 post 请求 webclient
how to issue a post request webclient under specific jwt context
如何使用 webclient 在下面的上下文中发出 POST 请求?
我能够从 ADFS 成功验证并检索 jwt
令牌:
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
}
如何在同一上下文中发出 POST 请求?
授权header应该可以添加到客户端以进行授权请求。
//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);
//...
如何使用 webclient 在下面的上下文中发出 POST 请求?
我能够从 ADFS 成功验证并检索 jwt
令牌:
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
}
如何在同一上下文中发出 POST 请求?
授权header应该可以添加到客户端以进行授权请求。
//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);
//...