简体   繁体   English

获取网站的SSL证书的公钥

[英]Get the public key of a website's SSL certificate

I'm not really sure about whether the following is doable or not because I'm in no way an expert on the subject (security, certificates...etc). 我不确定以下内容是否可行,因为我绝不是该主题(安全,证书等)的专家。

Anyway, what I want to do is get the Public Key of a website's SSL certificate using C# code. 无论如何,我要做的是使用C#代码获取网站SSL证书的公钥。 Like is there a way to query that information from the site using an HTTP request or something? 好像有一种方法可以使用HTTP请求或类似的方法从站点查询该信息?

In order for you guys to understand why I really want to do so, I'll briefly explain the scenario I want to make happen. 为了让大家理解我为什么要这么做,我将简要说明我想实现的情况。 Basically I have a bunch of websites that use OAuth 2.0 to achieve a state of trust among each other. 基本上,我有一堆网站使用OAuth 2.0来实现彼此之间的信任状态。 So let's say Site1 issued a request to Site2 and sent it a token which is supposedly from a trusted Authorization Server. 因此,假设Site1Site2发出了一个请求,并向其发送了一个令牌,该令牌应该来自受信任的授权服务器。 Site2 should be able to verify the authenticity of this token. Site2应该能够验证此令牌的真实性。

I hope that was clear enough. 我希望这很清楚。

I use the following block of code for a similar purpose. 我将以下代码块用于类似目的。 I hope it helps!!! 希望对您有所帮助!!!

        Uri u = new Uri(url);
        ServicePoint sp = ServicePointManager.FindServicePoint(u);

        string groupName = Guid.NewGuid().ToString();
        HttpWebRequest req = HttpWebRequest.Create(u) as HttpWebRequest;
        req.ConnectionGroupName = groupName;

        using (WebResponse resp = req.GetResponse())
        {

        }
        sp.CloseConnectionGroup(groupName);
        byte[] key = sp.Certificate.GetPublicKey();

        return key;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM