编程语言
首页 > 编程语言> > c# – 如何在特定的jwt上下文下发布post请求webclient

c# – 如何在特定的jwt上下文下发布post请求webclient

作者:互联网

如何使用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请求?

解决方法:

应该能够将授权标头添加到客户端以发出授权请求.

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

//...

标签:c,httpclient,jwt,dynamics-crm,dynamics-crm-2015
来源: https://codeday.me/bug/20190627/1304969.html