3

自分で作成したSSL証明書を使用してIISでFTPサーバーをセットアップしました( Makecert.exePvk2Pfxを使用)。PFXファイルをFTPサーバーに帰属させました。

FTPサーバーに接続するC#スクリプトがあり、常に次のエラーメッセージが表示されます。

System.Security.Authentication.AuthenticationException:検証手順に従って、リモート証明書が無効です。

ローカルコンピューターとユーザーの「信頼されたルート証明機関」に証明書をインストールしました。

認証されないため、ストアのC#で確認しました。

X509Store store = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

foreach (X509Certificate2 mCert in store.Certificates)
{
     var friendlyName = mCert.Issuer;
     Console.WriteLine(friendlyName);
}
store.Close();

しかし、私の証明書はリストされていません。MMCコンソールを開くと、証明書が表示されます。

4

2 に答える 2

3

通常、C#は、自己署名証明書の場合のように、信頼されたルート証明書がない証明書を信頼しません。はServicePointManager、自分で信頼を処理できる関数を追加することを可能にします。

// Callback used to validate the certificate in an SSL conversation
private static bool ValidateRemoteCertificate(
    object sender,
    X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors policyErrors)
{
    if (Convert.ToBoolean(ConfigurationManager.AppSettings["IgnoreSslErrors"]))
    {
        // Allow any old dodgy certificate...
        return true;
    }
    else
    {
        return policyErrors == SslPolicyErrors.None;
    }
}

private static string MakeRequest(string uri, string method, WebProxy proxy)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
    webRequest.AllowAutoRedirect = true;
    webRequest.Method = method;

    // Allows for validation of SSL conversations
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
    ValidateRemoteCertificate);

    if (proxy != null)
    {
        webRequest.Proxy = proxy;
    }

    HttpWebResponse response = null;
    try
    {
        response = (HttpWebResponse)webRequest.GetResponse();
        using (Stream s = response.GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(s))
            {
                return sr.ReadToEnd();
            }
        }
    }
    finally
    {
        if (response != null)
            response.Close();
    }
}

ブログ投稿から無効なSSL証明書をプログラムで受け入れる方法

于 2012-06-26T14:46:11.177 に答える
1

簡単な回避策として、次の方法ですべての証明書を受け入れることができます。

ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
于 2013-01-29T10:04:53.793 に答える