블로그 이미지
SQL Server VS. 석이 minsouk@hotmail.com MSSQL 쿼리성능 관련해 궁금한 사항이 있다면 언제나 누구나 TeamViewer + Line (네이버 japan 메신저) 에 minsouk1 추가 후 연락주세요~ 010-9967-0955 보미아빠

카테고리

보미아빠, 석이 (465)
밥벌이 (16)
싸이클 (1)
일상 (1)
Total247,222
Today3
Yesterday49

달력

« » 2020.8
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          

공지사항

* 모두삭제
del hostname.*

* private key 와 인증서 및 인증서 암호를 입력한다. 
makecert -r -len 2048 -pe -n "CN=HOSTNAME" -eku 1.3.6.1.5.5.7.3.1,1.3.6.1.5.5.7.3.2,1.3.6.1.5.5.7.3.3,1.3.6.1.5.5.7.3.4 -sky exchange -sv HOSTNAME.pvk HOSTNAME.cer -b 01/01/2005 -e 01/01/2035

* 암호입력
1234

* 포터블 파일로 변경한다. 
pvk2pfx -pvk HOSTNAME.pvk -spc HOSTNAME.cer -pfx HOSTNAME.pfx -pi 1234 -po 1234 -f

* pfx my, localhost 에 등록
* cer Root, localhost 에 등록 
certlm.mmc 에서 Personal + Trusted Root Certi 저장소에 HOSTNAME pfx 와 cert 파일이 있는지 확인한다. 
* 자동으로 등록하는 클래스 Certification.cs 확인 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using CsLib; 

namespace WcfHttpsServer
{
    class Certification
    {

        public bool Bind(string path, string pfx, string pfxpassword, string cert, string port)
        {
            try
            {
                string pfxFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path, pfx);
                string certFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path, cert);

                DeleteCertification(pfxFileName
                    , StoreName.My
                    , StoreLocation.LocalMachine
                    , pfxpassword
                    , X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

                DeleteCertification(certFileName
                    , StoreName.Root
                    , StoreLocation.LocalMachine);

                InstallCertification(pfxFileName
                    , StoreName.My
                    , StoreLocation.LocalMachine
                    , pfxpassword
                    , X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

                InstallCertification(certFileName
                    , StoreName.Root
                    , StoreLocation.LocalMachine);

                CertificationNetBind(certFileName, port);
            }
            catch (Exception)
            {
                throw;
            }
            return true;
        }

        private void InstallCertification(string certFileName, StoreName storeName, StoreLocation storeLocaion, string password, X509KeyStorageFlags x509KeyStorageFlags)
        {
            X509Store store = new X509Store(storeName, storeLocaion);
            try
            {
                bool Exists = false;
                X509Certificate2 cert = new X509Certificate2(certFileName, TranString.convertToSecureString(password), x509KeyStorageFlags);
                store.Open(OpenFlags.ReadWrite);
                foreach (X509Certificate2 storeCert in store.Certificates)
                {
                    if (storeCert.Thumbprint.Equals(cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
                    {
                        Exists = true;
                        break;
                    }
                }
                if (!Exists)
                {
                    store.Add(cert);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Close();
            }
        }

        private void InstallCertification(string certFileName, StoreName storeName, StoreLocation storeLocaion)
        {
            X509Store store = new X509Store(storeName, storeLocaion);
            try
            {
                bool Exists = false;
                X509Certificate2 cert = new X509Certificate2(certFileName);
                store.Open(OpenFlags.ReadWrite);
                foreach (X509Certificate2 storeCert in store.Certificates)
                {
                    if (storeCert.Thumbprint.Equals(cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
                    {
                        Exists = true;
                        break;
                    }
                }
                if (!Exists)
                {
                    store.Add(cert);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Close();
            }
        }

        private void DeleteCertification(string certFileName, StoreName storeName, StoreLocation storeLocaion, string password, X509KeyStorageFlags x509KeyStorageFlags)
        {
            X509Store store = new X509Store(storeName, storeLocaion);
            try
            {
                X509Certificate2 cert = new X509Certificate2(certFileName, TranString.convertToSecureString(password), x509KeyStorageFlags);
                store.Open(OpenFlags.ReadWrite);
                foreach (X509Certificate2 storeCert in store.Certificates)
                {
                    if (storeCert.Thumbprint.Equals(cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
                    {
                        store.Remove(cert);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Close();
            }
        }

        private void DeleteCertification(string certFile, StoreName storeName, StoreLocation storeLocaion)
        {
            X509Store store = new X509Store(storeName, storeLocaion);
            try
            {
                X509Certificate2 cert = new X509Certificate2(certFile);
                store.Open(OpenFlags.ReadWrite);
                foreach (X509Certificate2 storeCert in store.Certificates)
                {
                    if (storeCert.Thumbprint.Equals(cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
                    {
                        store.Remove(cert);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Close();
            }
        }



        private string GetCertHash(string certFileName)
        {
            using (X509Certificate2 cert = new X509Certificate2(certFileName))
                return cert.Thumbprint.ToLower();
        }

        private string GetAssemblyGuid()
        {
            var assembly = typeof(Program).Assembly;
            var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
            var id = attribute.Value;
            return id.ToLower();
        }

        private bool CertificationNetBind(string certFileName, string port)
        {
            StringBuilder sbPsiResults = new StringBuilder();
            ProcessStartInfo psi = new ProcessStartInfo() { CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true };
            bool isBounded = false;
                       
            // SHOW
            psi.FileName = "netsh";
            psi.Arguments = $"http show sslcert ipport=0.0.0.0:{port}";
            sbPsiResults.Clear();
            Process procHttpShow = Process.Start(psi);
            while (procHttpShow != null && !procHttpShow.StandardOutput.EndOfStream)
                sbPsiResults.Append(procHttpShow.StandardOutput.ReadLine());
            procHttpShow?.WaitForExit(2000);

            if (sbPsiResults.ToString().ToLower().Contains(GetCertHash(certFileName)) && sbPsiResults.ToString().ToLower().Contains(GetAssemblyGuid()))
            {
                // DELETE
                psi.FileName = "netsh";
                psi.Arguments = $"http delete sslcert ipport=0.0.0.0:{port}";
                sbPsiResults.Clear();
                Process procHttpDelete = Process.Start(psi);
                while (procHttpDelete != null && !procHttpDelete.StandardOutput.EndOfStream)
                    sbPsiResults.Append(procHttpDelete.StandardOutput.ReadLine());
                procHttpDelete?.WaitForExit(2000);

                if (sbPsiResults.ToString().ToLower().Contains("success"))
                    isBounded = false;
                else
                    throw new Exception($"http delete sslcert error : {sbPsiResults.ToString()}");
            }
            else
                isBounded = false;
            
            if (!isBounded)
            {
                // ADD
                psi.FileName = "netsh";
                psi.Arguments = $"http add sslcert ipport=0.0.0.0:{port} certhash={GetCertHash(certFileName).ToLower()} appid={{{GetAssemblyGuid()}}}";
                sbPsiResults.Clear();
                Process procHttpAdd = Process.Start(psi);
                while (procHttpAdd != null && !procHttpAdd.StandardOutput.EndOfStream)
                    sbPsiResults.Append(procHttpAdd.StandardOutput.ReadLine());
                procHttpAdd?.WaitForExit(2000);
                if (sbPsiResults.ToString().ToLower().Contains("error"))
                {
                    isBounded = false;
                    throw new Exception($"http add sslcert error : {sbPsiResults.ToString()}");
                }
                else
                    isBounded = true;
            }
            return isBounded;
        }
    }
}



Posted by 보미아빠

댓글을 달아 주세요

secureString

2019. 11. 4. 16:51
보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoFMemento
{
    class CareTaker
    {
        static void Main(string[] args)
        {
            List savedStates = new List();
            Originator originator = new Originator();
            originator.set("State1");
            originator.set("State2");
            savedStates.Add(originator.saveToMemento());
            originator.set("State3");
            savedStates.Add(originator.saveToMemento());
            originator.set("State4");
            originator.restoreFromMemento(savedStates[1]);
        }
    }

    class Originator
    {
        private string state; 
        public void set (string state)
        {
            this.state = state;
            Console.WriteLine("Originator: Setting state to " + state);
        }

        public Memento saveToMemento() // createMemento()
        {
            Console.WriteLine("Originator: Saving to Memento.");
            return new GoFMemento.Originator.Memento(this.state);
            // 예제에서는 string 이 쓰였는데, 실제로는 object 가 많이 쓰이니 serialize deserilize 를 이용하면 더 좋은 구현이 될 수 있다. 
        }

        public void restoreFromMemento (Memento memento) // restore (Memento)
        {
            this.state = memento.getSavedState();
            Console.WriteLine("Originator: State after restoring from Memento: " + state);
        }

        public sealed class Memento
        {
            private string state;
            public Memento (string stateToSave) // setState()
            {
                state = stateToSave; 
            }

            public string getSavedState() // getState()
            {
                return state; 
            }
        }
    }

}

Posted by 보미아빠

댓글을 달아 주세요

최근에 달린 댓글

최근에 받은 트랙백

글 보관함