.net标准2.0中的自签名证书

问题描述 投票:0回答:1

我的问题在于.net标准2.0,因为相同的代码似乎在.net框架上工作,原因我不完全确定。

问题是我想向服务器发出使用自签名证书的http请求。现在,在.net框架(特别是4.6.1)中通过它的方法是使用:

ServicePointManager.ServerCertificateValidationCallback = CustomValidation;

public static bool CustomValidation
            (object sender,
            X509Certificate certificate,
            X509Chain chain,
            SslPolicyErrors policyErrors)
        {
            return true;
        }

这解决了这个问题。但是,在.net标准中执行此操作似乎编译但是相同的错误(WinHttpException - 发生安全性错误)发生System.AggregateException HResult = 0x80131500消息=发生了一个或多个错误。 (发送请求时发生错误。)Source = StackTrace:位于C:\ Users \ Nick \中matrix_tester.Program.Main(String [] args)的System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) source \ repos \ matrix-tester \ Program.cs:第11行

内部异常1:HttpRequestException:发送请求时发生错误。

内部异常2:WinHttpException:发生安全性错误

我在这里结束了我的智慧。 ServicePointManager是否未在.net标准中使用?

c# post https get
1个回答
0
投票

ServicePointManager应该在2.0中可用。

免责声明。我不知道为什么你的代码不起作用。当我需要自动接受证书时,我总是使用黑客。它适用于2.0。但请记住,此脚本接受所有自签名证书,这违反了安全性。请自行决定使用。这是一个单身人士课程。只需在程序开头调用它,如下所示:

Certificates.Instance.GetCertificatesAutomatically();

它应该适用于整个程序。希望它能帮助你前进。

using System;
using System.Collections.Generic;
using System.Security;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Net.Security;

namespace test
{
    public sealed class Certificates
    {
        private static Certificates instance = null;
        private static readonly object padlock = new object();

        Certificates()
        {
        }

        public static Certificates Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Certificates();
                    }
                    return instance;
                }
            }
        }
        public void GetCertificatesAutomatically()
        {
            ServicePointManager.ServerCertificateValidationCallback +=
                new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors)
                    => { return true; });
        }

        private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            //Return true if the server certificate is ok
            if (sslPolicyErrors == SslPolicyErrors.None)
                return true;

            bool acceptCertificate = true;
            string msg = "The server could not be validated for the following reason(s):\r\n";

            //The server did not present a certificate
            if ((sslPolicyErrors &
                SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
            {
                msg = msg + "\r\n    -The server did not present a certificate.\r\n";
                acceptCertificate = false;
            }
            else
            {
                //The certificate does not match the server name
                if ((sslPolicyErrors &
                    SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch)
                {
                    msg = msg + "\r\n    -The certificate name does not match the authenticated name.\r\n";
                    acceptCertificate = false;
                }

                //There is some other problem with the certificate
                if ((sslPolicyErrors &
                    SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors)
                {
                    foreach (X509ChainStatus item in chain.ChainStatus)
                    {
                        if (item.Status != X509ChainStatusFlags.RevocationStatusUnknown &&
                            item.Status != X509ChainStatusFlags.OfflineRevocation)
                            break;

                        if (item.Status != X509ChainStatusFlags.NoError)
                        {
                            msg = msg + "\r\n    -" + item.StatusInformation;
                            acceptCertificate = false;
                        }
                    }
                }
            }

            //If Validation failed, present message box
            if (acceptCertificate == false)
            {
                msg = msg + "\r\nDo you wish to override the security check?";
                //          if (MessageBox.Show(msg, "Security Alert: Server could not be validated",
                //                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                acceptCertificate = true;
            }

            return acceptCertificate;
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.