How to create a minimal dummy X509Certificate2?

前端 未结 4 1937
暖寄归人
暖寄归人 2021-01-01 11:29

I\'m unit testing a .NET application; some of the unit tests involve programmatically generating X509Certificate2 objects.

I don\'t care about actual signing/private

4条回答
  •  长发绾君心
    2021-01-01 12:23

    There is one more simple way of doing it.

    1. Create a Certificate using MakeCert or export any existing certificate.
    2. Add the Certificate in Project (App_Data folder).
    3. Follow below steps to get the certificate.

    Code:

            string certificate = "cert.pfx";
            string certPath = string.Format("{0}/App_Data/{1}", HttpRuntime.AppDomainAppPath, certificate);
            byte[] bytes = Util.FileToArray(certPath);
            X509Certificate2 publicKey = new X509Certificate2(bytes, "somepassoword");
    

提交回复
热议问题