私はsslで使用したいASP.NET Web APIを持っています。現在、サーバーは自己署名証明書を使用しています。この時点では、httpとhttpsの両方が許可されています。私はhttpでうまく動作する非常に基本的なテストクライアントを持っていますが、httpsでは動作しません。私はhttpsで動作するように、以下のクライアントコードを変更する方法を知りたいです。現在IE、Firefox、Chromeはhttpとhttpsの両方を使用してWeb APIに接続できるため、クライアントコードだけでサーバー上のものを変更する必要はありません。クライアントコードは次のとおりです。ssl Web APIに接続する.NETクライアント
static void Main(string[] args)
{
try
{
if (args.Length != 1)
{
Console.WriteLine("Please provide a url, and only a url.");
return;
}
var url = new Uri(args[0]);
var urlAuthority = url.GetLeftPart(UriPartial.Authority);
var urlPathQuery = args[0].Substring(urlAuthority.Length);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(urlAuthority);
HttpResponseMessage response = client.GetAsync(urlPathQuery).Result;
if (response.IsSuccessStatusCode)
Console.WriteLine(response.Content.ReadAsAsync<string>().Result); // expecting scalar results only
else
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
catch (Exception ex)
{
Exception tempEx = ex;
while (tempEx != null)
{
Console.WriteLine(tempEx.Message);
tempEx = tempEx.InnerException;
}
}
}
私のhttp URLで上記のコードを実行すると、うまくいきます。私はhttpsにURLを変更すると、次のエラーが出力されます。私が持っている
One or more errors occurred.
An error occurred while sending the request.
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
The remote certificate is invalid according to the validation procedure.
一つの疑問は、それが暗号化された応答を返送する方法を知っているように、クライアントは、手動でサーバーに公開鍵を送信しなければならない場合である場合、またはこれは自動的に舞台裏で行われますか?そして2番目に、最後のエラーを見た後、私はクライアント内でcertの警告を無視して、この証明書が進んでいいということを信じるために何か他のことをしなければならないのだろうか?
コンフィグレーションでトランスポートに設定されているセキュリティモードはありますか? – Joe
別の例がここにあります:http://stackoverflow.com/a/42449178/112397 – TombMedia