x509 Certificate for localhost in windows for using AuthenticateAsServer()

768 views Asked by At

hi I want to Create X509Certificate To Use in SslStream.AuthenticateAsServer() on windows 10 loopbak. I try to Create Certificate with OpenSsl and Makecert Script but certificates not work. please help me. my Code :

X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
                    store.Open(OpenFlags.ReadOnly);

                    RawStringData = RawStringData.Replace("CONNECT ", "GET https://");
                    RawStringData = RawStringData.Replace(":443", "/");
                    RawStringData = RawStringData.Replace("Proxy-Connection", "Connection");
                    RawByteArrayData = Encoding.ASCII.GetBytes(RawStringData);
                    Port = 443;
                    ServerSocket.Connect(HostEntry.AddressList[0], Port);
                    HttpsStream = new SslStream(new NetworkStream(ServerSocket));
                    HttpsStream.AuthenticateAsClient(Host);
                    HttpsStream.Write(RawByteArrayData);
                    int Count = HttpsStream.Read(NewData, 0, NewData.Length);
                    string pfxpath = @"D:\test.pfx";
                    X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxpath));
                    byte[] pfxData = File.ReadAllBytes(pfxpath);

                    cert = new X509Certificate2(pfxData,"", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

                    X509Certificate x509 = store.Certificates.Find(X509FindType.FindByIssuerName, "localhost", false)[0];
                    ssl = new SslStream(new NetworkStream(A) , false , Verification , null );
                    ssl.AuthenticateAsServer(cert , false, System.Security.Authentication.SslProtocols.Tls, true);
                    ssl.Write(NewData);
0

There are 0 answers