Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确保HttpWebRequest的安全,以便我可以发送凭据吗?_C#_Asp.net - Fatal编程技术网

C# 确保HttpWebRequest的安全,以便我可以发送凭据吗?

C# 确保HttpWebRequest的安全,以便我可以发送凭据吗?,c#,asp.net,C#,Asp.net,我有以下代码连接到我的php服务器并从中检索数据。唯一的问题是,我需要将用户名和密码从这个webrequest安全地发送到PHP服务器。查看webrequest类的文档,有一个credentials属性和一个preauthenticate属性。我假设这些是网络凭据(我的所有用户都在AD中) 是否可以使用凭据保护此post请求,或者这只是一个坏主意?我还发现了SetBasicAuthHeader——我会仔细阅读,看看它是否有帮助。所有流量都将通过SSL从ASPX站点传输到PHP站点 //

我有以下代码连接到我的php服务器并从中检索数据。唯一的问题是,我需要将用户名和密码从这个webrequest安全地发送到PHP服务器。查看webrequest类的文档,有一个credentials属性和一个preauthenticate属性。我假设这些是网络凭据(我的所有用户都在AD中)

是否可以使用凭据保护此post请求,或者这只是一个坏主意?我还发现了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();
    }

使用HTTPS时,您的数据在消息和传输范围内是安全的。这意味着没有人可以解码或嗅探数据包。我建议您阅读这篇文章

SetBasicAuthHeader是为您准备的,因此在这里没有帮助,因为您正在处理应用程序级别的身份验证。实际上,这并不比在浏览器中访问页面更不安全。我看到您正在使用SSL,因此您的请求将被加密,您无需担心

如果您出于其他原因(尽管我不知道为什么)担心,那么听起来您可以控制PHP端,这样您就可以加密密码并添加额外的POST参数,以便服务器知道如何解密密码