【发布时间】:2019-09-22 00:20:27
【问题描述】:
在我的应用程序中,我使用以下代码来验证客户端证书
public static async Task<string> CallApi(string url, Context context)
{
var hostName = "mytestapp.azurewebsites.net";
var port = 443;
Stream keyin = Application.Context.Assets.Open("Server.pfx");
var password = "pass123";
using (MemoryStream memStream = new MemoryStream())
{
keyin.CopyTo(memStream);
var certificates = new X509Certificate2Collection(new X509Certificate2(memStream.ToArray(), password));
await Task.Run(() =>
{
// Create a TCP/IP client socket.
// machineName is the host running the server application.
TcpClient client = new TcpClient(hostName, port);
Console.WriteLine("Client connected.");
// Create an SSL stream that will close the client's stream.
SslStream sslStream = new SslStream(
client.GetStream(),
false,
ValidateServerCertificate);
// The server name must match the name on the server certificate.
try
{
sslStream.AuthenticateAsClient(hostName, certificates, SslProtocols.Tls12, true);
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}
});
}
return string.Empty;
}
认证成功后,我想发出HTTP get请求。
sslStream.AuthenticateAsClient(hostName, certificates, SslProtocols.Tls12, true);
在此声明之后。比如说我需要在下面调用 http Get call
https://mytestapp.azurewebsites.net/api/GetUserProfile?userId="Sooraj"
如何调用此调用?或者是否有可能实现相同的?
请帮忙
【问题讨论】:
-
使用 WebClient 可能比您自己的 TcpClient 更容易。您可以允许 WebClient 使用证书和 SSL。示例:stackoverflow.com/questions/2066489/…
-
另一种选择可能是 HttpWebRequest。这是带有客户端证书的示例:stackoverflow.com/questions/39528973/…
-
否则,如果你继续使用TcpClient,我相信你需要编写自己的HTTP头和请求流,并手动解析响应。
-
很遗憾我正在使用 Mono / Xamarin。我无法使用 WebClient。 Mono.Net 中存在一个已知限制
-
能否请您告诉我有关继续使用 TCP 客户端的更多信息。
标签: c# .net tcpclient sslstream