Install SSL certificate programmatically using Microsoft.Web.Administration

前端 未结 7 1749
执念已碎
执念已碎 2020-12-28 20:11

So the Microsoft.Web.Administration API is very easy to use to create HTTP and HTTPS bindings for sites:

using (ServerManager manager = new          


        
7条回答
  •  抹茶落季
    2020-12-28 20:29

    As Helephant's answer is the best if you need the certificate hashes (i.e. multiple IPs on a single machine with various SSL certs), you'll need to know how to get the certificates/hashes. The few lines below demonstrate how to find the information out, as the MSDN documentation is so poor for this subject.

    You can't remotely update an SSL binding using ServerManager.OpenRemote() - there appears to be a bug with this. Appcmd won't help you either.

    If you want to convert a byte string back into a byte array (if you know the hash), here's how.

    static void Main(string[] args)
    {
        var store2 = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);
        Console.WriteLine("TrustedPublisher:");
        PrintCerts(store2);
        Console.WriteLine(); 
    
        Console.WriteLine("MY:");
        store2 = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        PrintCerts(store2);
        Console.WriteLine();
    
        Console.WriteLine("CertificateAuthority:");
        store2 = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine);
        PrintCerts(store2);
        Console.WriteLine();
    }
    
    static string PrintHash(byte[] cert)
    {
        StringBuilder builder = new StringBuilder();
    
        foreach (byte b in cert)
        {
            builder.AppendFormat("{0:x2}", b);
        }
    
        return builder.ToString();
    }
    
    static void PrintCerts(X509Store store)
    {
        store.Open(OpenFlags.OpenExistingOnly);
        foreach (var cert in store.Certificates)
        {
            Console.Write("{0} - {1}", cert.FriendlyName, PrintHash(cert.GetCertHash()));
            Console.WriteLine();
        }
    }
    

    Example output:

    MY:
    www.awesomesite.com - cc2b5fc8216a949b58aadc21089c12b2c090f6bd

提交回复
热议问题