Get the public key of an SSL website certificate

I am not sure whether the following is feasible or not, because I am by no means an expert on this issue (security, certificates ... etc.).

In any case, I want to get the public key of the website SSL certificate using C # code. How is there a way to request this information from a site using an HTTP request or something else?

So that you guys understand why I really want this, I will briefly explain the scenario that I want to do. Basically, I have a bunch of websites that use OAuth 2.0 to achieve trust in each other. So, let Site1him send a request to Site2and send him a token, presumably from a trusted authorization server. Site2must be able to verify the authenticity of this token.

Hope this was clear enough.

+3
source share
1 answer

I use the following code block for a similar purpose. Hope this 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;
+4
source

All Articles