我有以下代码连接到我的 php 服务器并从中检索数据。唯一的事情是,我需要将用户名和密码从这个 web 请求安全地发送到 PHP 服务器。查看 webrequest 类的文档,有一个 credentials 属性和一个 preauthenticate 属性。我假设这些是用于网络凭据(我所有的用户都在 AD 中)。
是否可以使用凭据来保护此发布请求,或者这只是一个坏主意?我还找到了 SetBasicAuthHeader - 我会阅读这个,看看它是否有帮助。所有流量都将通过 SSL 从 ASPX 站点到 PHP 站点
// variables to store parameter values
string url = "https://myphpserver.php";
// creates the post data for the POST request
string postData = "Username=" + username + "&Password=" + "&UID=" + UniqueRecID;
// create the POST request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
// POST the data
using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter2.Write(postData);
}
// This actually does the request and gets the response back
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
string responseData = string.Empty;
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
// dumps the HTML from the response into a string variable
responseData = responseReader.ReadToEnd();
}