Exemple #1
0
        private X509SecurityToken getToken(string which)
        {
            X509SecurityToken    token = null;
            X509CertificateStore store = null;

            string serverKeyIdentifier = "bBwPfItvKp3b6TNDq+14qs58VJQ=";             //"po3h4Y4J8ITs/pW3acuRjpT8V1o=";
            string clientKeyIdentifier = "gBfo0147lM6cKnTbbMSuMVvmFY4=";             //"Gu4aD7+bYTVtmSveoPIWTRtzD3M=";

            store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);
            store.OpenRead();
            X509CertificateCollection coll;

            if (which == "server")
            {
                coll = store.FindCertificateByKeyIdentifier(Convert.FromBase64String(serverKeyIdentifier));
            }
            else
            {
                coll = store.FindCertificateByKeyIdentifier(Convert.FromBase64String(clientKeyIdentifier));
            }

            if (coll.Count > 0)
            {
                X509Certificate cert = (X509Certificate)coll[0];
                token = new X509SecurityToken(cert);
                byte[] hash         = cert.GetCertHash();
                string hashstring   = cert.GetCertHashString();
                string serialstring = cert.GetSerialNumberString();
            }
            return(token);
        }
        public static X509SecurityToken GetClientToken()
        {
            X509SecurityToken token = null;
            // Open the CurrentUser Certificate Store and try MyStore only
            X509CertificateStore store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);

            token = RetrieveTokenFromStore(store, ClientBase64KeyId);
            return(token);
        }
        public static X509SecurityToken GetServerToken()
        {
            X509SecurityToken    token = null;
            X509CertificateStore store = null;

            store = X509CertificateStore.CurrentUserStore(X509CertificateStore.OtherPeople);
            token = RetrieveTokenFromStore(store, ServerBase64KeyId);

            //
            // If we failed to retrieve it from the OtherPeople,
            // we now try the MyStore
            //
            if (token == null)
            {
                store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);
                token = RetrieveTokenFromStore(store, ServerBase64KeyId);
            }
            return(token);
        }
        public void EncryptAckResponse()
        {
            //Open the current user certificate store and look for Personal category
            X509CertificateStore localStore = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);

            localStore.OpenRead();

            //Find Vendor A Certificate
            X509CertificateCollection certCollection = localStore.FindCertificateBySubjectString("Vendor B");
            X509Certificate           provCert       = certCollection[0];

            //Create a new security token that is of X509 type
            //Token represent claim (authentication information)
            X509SecurityToken token = new X509SecurityToken(provCert);

            ResponseSoapContext.Current.Security.Tokens.Add(token);

            //Instruct WSE inbound filter to encrypt the message before it is transmitted over wire
            ResponseSoapContext.Current.Security.Elements.Add(new EncryptedData(token));
        }