IIS Express使用CLI信任自签名SSL证书

时间:2018-03-30 15:27:07

标签: c# ssl iis asp.net-web-api makecert

我们正在Visual Studio中开发Web API Web服务器。我们启用了SSL。这需要本地SSL证书。我们已经在我们的开发机器上进行了设置,但我们需要能够通过命令行在我们的CI构建机器上进行设置,以便运行Selenium测试。在本地,Visual Studio有助于实现这一目标。启动Web API Web服务器时,会出现以下提示:

This project is configured to use SSL. To avoid SSL warnings in the browser you can choose to trust the self-signed certificate that IIS Express has generated. Would you like to trust the IIS Express SSL certificate?

我需要复制通过命令行在此提示中单击“是”时发生的情况。我该怎么做?

1 个答案:

答案 0 :(得分:0)

以下C#代码与Visual Studio完全相同(取自Jexus Manager,https://github.com/jexuswebserver/JexusManager/blob/master/JexusManager.Features.Certificates/CertificatesFeature.cs

        private void Trust()
        {
            var cert = SelectedItem.Certificate;
            var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadWrite);
            if (store.Certificates.Find(X509FindType.FindByThumbprint, cert.Thumbprint, false).Count == 0)
            {
                try
                {
                    store.Add(cert);
                }
                catch (CryptographicException ex)
                {
                    if (ex.HResult != NativeMethods.UserCancelled)
                    {
                        var dialog = (IManagementUIService)GetService(typeof(IManagementUIService));
                        dialog.ShowMessage($"An unexpected error happened. HResult is {ex.HResult}. Contact your system administrator.", Name,
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    // add operation cancelled.
                }
            }

            store.Close();
        }

将其翻译为PowerShell或任何等效命令,然后您就可以实现目标。