首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell与PowerShell核的PowerShell差异

Powershell与PowerShell核的PowerShell差异
EN

Server Fault用户
提问于 2019-09-20 00:25:25
回答 2查看 2K关注 0票数 2

希望有人能弄清楚这件事。在Powershell中,以下代码生成网站证书的输出:

代码语言:javascript
复制
$req = [Net.HttpWebRequest]::Create('https://www.google.com')
$req.GetResponse()
$req.Servicepoint.certificate

       Handle Issuer                                       Subject
       ------ ------                                       -------
2266454817712 CN=GTS CA 1O1, O=Google Trust Services, C=US CN=www.google.com...

在Powershell Core中,它在$req.Servicepoint.certificate中返回空/空。

代码语言:javascript
复制
BindIPEndPointDelegate :
ConnectionLeaseTimeout : -1
Address                : https://www.google.com/
MaxIdleTime            : 100000
UseNagleAlgorithm      : True
ReceiveBufferSize      : -1
Expect100Continue      : True
IdleSince              : 20/09/2019 9:42:48 AM
ProtocolVersion        : 1.1
ConnectionName         : https
ConnectionLimit        : 2
CurrentConnections     : 0
Certificate            :
ClientCertificate      :
SupportsPipelining     : True
EN

回答 2

Server Fault用户

发布于 2019-09-20 05:35:22

看起来,.NET Core 第36979期很好地解释了这一点。

简而言之,System.Net.HttpWebRequest已经过时,只在.NET核心中部分地重新实现了。特别是,ServicePoint和ServicePointManager类是存在的,但实际上不起作用。

他们建议迁移到System.Net.Http.HttpClient,一个用户提供了下面的代码示例来演示如何使用它检索证书。

代码语言:javascript
复制
using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace NetCoreConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = CustomCallback;
            var client = new HttpClient(handler);

            HttpResponseMessage response = client.GetAsync("https://www.google.com.mx/").GetAwaiter().GetResult();
            Console.WriteLine(response.StatusCode);
            Console.WriteLine((int)response.StatusCode);
        }

        private static bool CustomCallback(HttpRequestMessage arg1, X509Certificate2 arg2, X509Chain arg3, SslPolicyErrors arg4)
        {
            Console.WriteLine(arg2.GetEffectiveDateString());
            Console.WriteLine(arg2.GetExpirationDateString());
            Console.WriteLine(arg2.Issuer);
            Console.WriteLine(arg2.Subject);

            return arg4 == SslPolicyErrors.None;
        }
    }
}
票数 1
EN

Server Fault用户

发布于 2019-09-20 10:20:16

如果有人感兴趣,下面是我为PS Core重新设计的方法:

代码语言:javascript
复制
$Req = [System.Net.Sockets.TcpClient]::new('www.google.com', '443')
$Stream = [System.Net.Security.SslStream]::new($Req.GetStream())
$Stream.AuthenticateAsClient('www.google.com')
$Stream.RemoteCertificate
$Stream.RemoteCertificate.GetExpirationDateString()

(等)

Ssl​流.​远程​证书属性

票数 1
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/984953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档