Generate a self-signed certificate on the fly

前端 未结 5 888
夕颜
夕颜 2020-11-27 11:27

I searched around, but I didn\'t find a clear example. I want to create a self-signed (self-)trusted certificate programmatically (C#), following these step

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 11:41

    So after a long time I refine my answer. My original task was to create a certificate on the fly for a WCF service.

    My Windows service application has a WCF service and I want to create the channel at runtime with the creation of the associated certificate if it's not present on the local machine.

    All the above code is the right way to do this, but there's one piece missing.

    You have to create a certificate with an embedded exportable private key.

    See this link.

    This is the focus on the critical part of the snippet to generate the private key:

    var cspParams = new CspParameters
    {
          KeyContainerName = Guid.NewGuid().ToString(),
          KeyNumber = (int)KeyNumber.Exchange,
          Flags = CspProviderFlags.UseMachineKeyStore
    };
    

    If you don't do this when you execute the netsh command, you get the error 1312. For example, of the netsh command:

    netsh http add sslcert ipport=192.168.0.15:8081 certhash=‎5424476237fc2785ed2d0fd620a9131d7c999f6f appid={02639d71-0935-35e8-9d1b-9dd1a2a34627}
    

提交回复
热议问题